GraphQL

This page refers to the GraphQL JavaScript package

Directory Structure

src/graphql/Schema.ts
src/graphql/Mutation.ts
src/graphql/User.ts (GraphQLObjectType)
src/graphql/mutations/UserMutation.ts

Schema

Put edges in a "Viewer" GraphQLObjectType so that you can use it as the "root" object.

type Query {
entries(order: String, sort: String, after: String, first: Int, before: String, last: Int): EntryConnection
tags(after: String, first: Int, before: String, last: Int): TagConnection
viewer: Viewer
}
type Viewer implements EntryConnectionInterface {
entries(order: String, sort: String, after: String, first: Int, before: String, last: Int): EntryConnection
tags(after: String, first: Int, before: String, last: Int): TagConnection
}

Resolvers

Naming:

  • Postfix function names with Resolver

  • Pass in all the args: root, args, context, info

Entry.ts
// For the connections
`query {
entries(...) {
edges...
}
}`
export async function EntriesResolver(root, args, context, info);
`query {
entry(...) { }
}`
export async function EntryResolver(root, args, context, info);
// My suggestion is put this function in the `Tag.ts` file.
// The goal is put all Resolvers that query the same database in the same file
`query {
entry(...) {
tags(...) {
edges...
}
}
}`
export async function EntryTagsResolver(root, args, context, info) {
const row = await knex
.from("tags")
.where({
tag.entryID: root.id,
})
.select();
...
}
export async function EntryResolver(
root,
args,
context: Context,
info
): Promise<any> {
const userUUID = getUserUUIDFromContext(context);
const where = {
uuid: args.uuid,
createdBy: userUUID
};
const row = await knex
.from("users")
.where(where)
.limit(1)
.select();
return row.shift();
}

Bugs

Could not convert from GraphQL type String

When running relay-compiler I would get this error:

Could not convert from GraphQL type String

Environment: Lerna with multiple graphql packages installed

Solution specify resolutions in local package.json:

package.json
"resolutions": {
"graphql": $your_version
}

Worse case, nuke your node_modules:

rm -rf \
packages/your-package/node_modules/ \
node_modules/ \
yarn.lock