0.1.1 • Published 6 years ago
relay-connection-count v0.1.1
relay-connection-count
This helper generates a count
field in your GraphQL schema next to a Relay connection.
Installation
npm install --save relay-connection-count
# or
yarn add relay-connection-count
Warning: This package expects graphql
and graphql-relay
to be already installed.
Usage
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!
}
API
The parameter of the createFields
function is an object containing following keys:
prefix: string
: the prefix used to generate field name. For example, prefixproject
will outputprojects
andprojectCount
fields.connectionType: GraphQLObjectType
: the type of the connection (most likely generated withconnectionDefinitions
).resolveConnection: Function
: takes the same parameters as a usualresolve
function, and should return either an array of elements or a promised array.extraArgs?: ?{[string]: { type : GraphQLInputType }}
: a usual graphql arguments' definition.