GraphQL
This page refers to the GraphQL JavaScript package

Directory Structure

1
src/graphql/Schema.ts
2
src/graphql/Mutation.ts
3
src/graphql/User.ts (GraphQLObjectType)
4
src/graphql/mutations/UserMutation.ts
Copied!

Schema

Put edges in a "Viewer" GraphQLObjectType so that you can use it as the "root" object.
1
type Query {
2
entries(order: String, sort: String, after: String, first: Int, before: String, last: Int): EntryConnection
3
tags(after: String, first: Int, before: String, last: Int): TagConnection
4
viewer: Viewer
5
}
6
7
type Viewer implements EntryConnectionInterface {
8
entries(order: String, sort: String, after: String, first: Int, before: String, last: Int): EntryConnection
9
tags(after: String, first: Int, before: String, last: Int): TagConnection
10
}
Copied!

Resolvers

Naming:
  • Postfix function names with Resolver
  • Pass in all the args: root, args, context, info
Entry.ts
1
// For the connections
2
`query {
3
entries(...) {
4
edges...
5
}
6
}`
7
export async function EntriesResolver(root, args, context, info);
8
9
`query {
10
entry(...) { }
11
}`
12
export async function EntryResolver(root, args, context, info);
13
14
// My suggestion is put this function in the `Tag.ts` file.
15
// The goal is put all Resolvers that query the same database in the same file
16
`query {
17
entry(...) {
18
tags(...) {
19
edges...
20
}
21
}
22
}`
23
export async function EntryTagsResolver(root, args, context, info) {
24
const row = await knex
25
.from("tags")
26
.where({
27
tag.entryID: root.id,
28
})
29
.select();
30
31
...
32
}
Copied!
1
export async function EntryResolver(
2
root,
3
args,
4
context: Context,
5
info
6
): Promise<any> {
7
const userUUID = getUserUUIDFromContext(context);
8
9
const where = {
10
uuid: args.uuid,
11
createdBy: userUUID
12
};
13
14
const row = await knex
15
.from("users")
16
.where(where)
17
.limit(1)
18
.select();
19
return row.shift();
20
}
Copied!

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
1
"resolutions": {
2
"graphql": $your_version
3
}
Copied!
Worse case, nuke your node_modules:
1
rm -rf \
2
packages/your-package/node_modules/ \
3
node_modules/ \
4
yarn.lock
Copied!
Last modified 3yr ago