Relay
Relay support
graphql-thinky has built in support for RelayJs. There few things to see to be ready and use Relay complaint entry points.
Map Types
An important piece is to bind the models with they're own GraphQLTypes so that the library can figure out how to retrieve Types based to a relay globalD.
Note: If you use the function createModelType this is done internally for you. So you don't have to manually bind your models. If not then as following:
//graphql-thinky.js
import graphqlThinky from 'graphql-thikny';
import thinky from './thinky';
const GraphQLThinky = new graphqlThinky(thinky);
GraphQLThinky.nodeTypeMapper.mapTypes({
user: userType,
custom: {
type: customType,
resolve(globalId, context) {
const { id } = fromGlobalId(globalId);
return resolveIdFromSomewhere(id);
}
}
});
Second we need to assign the globalID + nodeField interface to our Type.
Again if you use createModelType function will be as easy as set the option globalId: true. If instead you create your types manually:
import graphqlThinky from './graphql-thikny';
import { GraphQLObjectType } from 'graphql;
import { globalIdField } from 'graphql-relay';
export default new GraphQLObjectType({
name: 'user',
fields: () => ({
id: globalIdField('user')
}),
interfaces: () => [graphqlThinky.nodeInterface]
});
Resolving Connection
Let's see now how to resolve connections based on the relay definition.
Here is our userQuery.js
//userQuery.js
import GT from './graphql-thinky';
import userType from './userType';
const { connect } = GT;
export default {
usersConnection: {
...connect('user',null, {
connection: {
name: ' UserConnection',
type: userType
}
})
}
}
We are now ready to test our connection with the following GraphQL query:
{
usersConnection {
edges {
node {
id
userID
name
}
}
}
}
You can also use all the relay arguments, such as: first, last, after, before
Connect = (modelName, related, opts = {})
The connection function is similar to the resolve function. Let's look it's arguments
- modelName:
string
- Thinky model name - Required - related:
string|nil
- Relation name of the model name provided - opts:
Object
:- args:
Object
- provide additional arguments - connection:
Object
- connection info - Required- name:
string
- name of the connection - Required - type:
GraphQLObjectType
type of the connection - Required - before:
Function
- before hook - after:
Function
- after hook
- name:
- args:
Updated less than a minute ago