This helper generates a count field in your GraphQL schema next to a Relay connection.
npm install --save relay-connection-count
# or
yarn add relay-connection-count
Warning: This package expects graphql and graphql-relay to be already installed.
import { createFields } from "relay-connection-count";
// ...
const userType = new GraphQLObjectType({
name: "User",
fields: () => ({
id: globalIdField("User", u => u._id),
name: {
type: new GraphQLNonNull(GraphQLString),
},
...createFields({
prefix: "project",
connectionType: userConnection,
resolveConnection: u => u.projects,
}),
}),
});This should give a schema looking like:
type User {
id: ID!
name: String!
projects(
after: String
first: Int
before: String
last: Int
): BatchConnection
projectCount: Int!
}The parameter of the createFields function is an object containing following keys:
prefix: string: the prefix used to generate field name. For example, prefixprojectwill outputprojectsandprojectCountfields.connectionType: GraphQLObjectType: the type of the connection (most likely generated withconnectionDefinitions).resolveConnection: Function: takes the same parameters as a usualresolvefunction, and should return either an array of elements or a promised array.extraArgs?: ?{[string]: { type : GraphQLInputType }}: a usual graphql arguments' definition.