Skip to content

Instantly share code, notes, and snippets.

@nijynot
Created December 23, 2015 13:22
Show Gist options
  • Save nijynot/3f03d08d8880f75375b0 to your computer and use it in GitHub Desktop.
Save nijynot/3f03d08d8880f75375b0 to your computer and use it in GitHub Desktop.
import {
GraphQLInt,
GraphQLObjectType,
GraphQLString
} from 'graphql';
import {
fromGlobalId,
toGlobalId,
globalIdField
} from 'graphql-relay';
import { registerType } from './registry';
import { nodeInterface } from './node.js';
import {
getParent,
getChild,
getAllParents,
getAllChilds
} from '../database.js';
import {parentType} from './parentType';
export const ChildType = registerType(new GraphQLObjectType({
name: 'Child',
fields: () => ({
id: globalIdField('Child'),
name: {
type: GraphQLString
},
type: {
type: GraphQLString
},
parent: {
type: parentType,
resolve: (root) => {
return getParent(root.id);
}
}
}),
interfaces: [nodeInterface]
}));
var childs = [
{ id: 0, name: 'Child 0' },
{ id: 1, name: 'Child 1' },
{ id: 2, name: 'Child 2' },
{ id: 3, name: 'Child 3' },
{ id: 4, name: 'Child 4' }
];
var parents = [
{ id: 0, name: 'Parent 0' },
{ id: 1, name: 'Parent 1' },
{ id: 2, name: 'Parent 2' },
{ id: 3, name: 'Parent 3' },
{ id: 4, name: 'Parent 4' }
];
export function getParent(id) {
return parents[id];
}
export function getChild(id) {
return childs[id];
}
export function getAllParents() {
return parents;
}
export function getAllChilds() {
return childs;
}
import {
fromGlobalId,
toGlobalId,
nodeDefinitions
} from 'graphql-relay';
import {
getParent,
getChild,
getAllParents,
getAllChilds
} from '../database.js';
import { idFetcher, typeResolver } from './registry.js';
export const { nodeInterface, nodeField } = nodeDefinitions(
idFetcher, typeResolver );
import {
GraphQLInt,
GraphQLObjectType,
GraphQLString
} from 'graphql';
import {
fromGlobalId,
toGlobalId,
globalIdField
} from 'graphql-relay';
import { registerType } from './registry';
import { nodeInterface } from './node.js';
import {
getParent,
getChild,
getAllParents,
getAllChilds
} from '../database.js';
import {childType} from './childType';
export const parentType = registerType(new GraphQLObjectType({
name: 'Parent',
fields: () => ({
id: globalIdField('Parent'),
name: {
type: GraphQLString
},
type: {
type: GraphQLString
},
child: {
type: childType,
resolve: (root) => {
return getChild(root.id);
}
}
}),
interfaces: [nodeInterface]
}));
import decamelize from 'decamelize';
import { fromGlobalId } from 'graphql-relay';
import pluralize from 'pluralize';
import {
getParent,
getChild,
getAllParents,
getAllChilds
} from '../database.js';
const types = {};
const endpoints = {};
const getItemOverrides = {};
export function getEndpoint(type) {
return endpoints[type];
}
function getDefaultEndpoint(type) {
const endpoint = pluralize(decamelize(type.name));
return id => id ? `${endpoint}/${id}` : endpoint;
}
export function registerType(type, endpoint, getItemOverride) {
types[type.name] = type;
endpoints[type] = endpoint || getDefaultEndpoint(type);
getItemOverrides[type] = getItemOverride;
// Allow e.g. `export default registerType(MyType);`.
return type;
}
export async function idFetcher(globalId, info) {
const { type, id } = fromGlobalId(globalId);
const getItemOverride = getItemOverrides[type];
let item;
if (getItemOverride) {
item = await getItemOverride(id, info);
} else {
if (type === 'Parent') {
console.log(type);
item = await getParent(id);
} else if (type === 'Child') {
console.log(type);
item = await getChild(id);
} else {
console.warn('Warning: type not handled', type);
item = null;
}
}
await console.log({ type, ...item });
return { type, ...item };
}
export function typeResolver(obj) {
return types[obj.type];
}
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString
} from 'graphql';
import {
connectionArgs,
connectionDefinitions,
connectionFromArray,
connectionFromPromisedArray,
fromGlobalId,
toGlobalId,
globalIdField,
mutationWithClientMutationId,
cursorForObjectInConnection,
nodeDefinitions
} from 'graphql-relay';
import {
getParent,
getChild,
getAllParents,
getAllChilds
} from './database.js';
import { nodeField } from './types/node.js';
import {childType} from './types/childType.js';
var queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
child: {
type: childType,
args: {
id: GraphQLInt
},
resolve: (_, args) => {
return getChild(args.id);
}
}
})
});
var Schema = new GraphQLSchema({
query: queryType
});
module.exports = Schema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment