Created
March 2, 2018 08:02
-
-
Save dclarke-modus/07c277d51e65dcd53621f3008e5fef65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {GraphQLSchema, GraphQLObjectType, GraphQLInt, GraphQLString, GraphQLList, GraphQLBoolean} from 'graphql'; | |
import {connectionArgs, connectionDefinitions, connectionFromArray, connectionFromPromisedArray, fromGlobalId, globalIdField, mutationWithClientMutationId, nodeDefinitions} from 'graphql-relay'; | |
import {ClientModel, ScopeModel, TokenModel, GrantModel} from '../schemas'; | |
let Schema = () => { | |
let {nodeInterface, nodeField} = nodeDefinitions( | |
(globalId) => { | |
var {type, id} = fromGlobalId(globalId); | |
if (type === 'Grant') { | |
return GrantModel.findById(id); | |
} else if (type === 'Scope') { | |
return ScopeModel.findById(id); | |
} else { | |
return null; | |
} | |
}, | |
(obj) => { | |
if (obj instanceof ScopeModel) { | |
return scopeType; | |
} else if (obj instanceof GrantModel) { | |
return grantType; | |
} else { | |
return null; | |
} | |
} | |
); | |
let grantType = new GraphQLObjectType({ | |
name: 'Grant', | |
fields: () => ({ | |
id: globalIdField('Grant'), | |
name: {type: GraphQLString}, | |
description: {type: GraphQLString} | |
}), | |
interface : [nodeInterface] | |
}); | |
let scopeType = new GraphQLObjectType({ | |
name:'Scope', | |
fields: () => ({ | |
id: globalIdField('Scope'), | |
description: {type: GraphQLString}, | |
name: {type: GraphQLString}, | |
enabled: {type: GraphQLBoolean}, | |
grants: {type: new GraphQLList(grantType)} | |
}) | |
}); | |
let {connectionType: scopeConnection} = connectionDefinitions({nodeType: scopeType}); | |
let schema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'Query', | |
fields: () => ({ | |
scopes: { | |
type: scopeConnection, | |
args: connectionArgs, | |
resolve: (scope, args) => connectionFromPromisedArray(ScopeModel.find({}), args) | |
} | |
}) | |
}) | |
/*mutation: new GraphQLObjectType({ | |
name: 'Mutation', | |
fields: () => ({ | |
incrementCounter: { | |
type: GraphQLInt, | |
resolve: () => 4 | |
} | |
}) | |
})*/ | |
}); | |
return schema; | |
} | |
export default Schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment