Created
March 6, 2018 21:57
-
-
Save dclarke-modus/b377d534cd26fa0fefde2e1e96bde463 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
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 {connectionType: grantConnection} = connectionDefinitions({nodeType: grantType}); | |
var scopeMutation = mutationWithClientMutationId({ | |
name: 'AddScope', | |
inputFields: { | |
id : { | |
type: GraphQLString | |
}, | |
description: { | |
type: new GraphQLNonNull(GraphQLString) | |
}, | |
name: { | |
type: new GraphQLNonNull(GraphQLString) | |
}, | |
enabled : { | |
type: new GraphQLNonNull(GraphQLBoolean) | |
}, | |
grants : { | |
type: new GraphQLList(grantType) | |
} | |
}, | |
outputFields: { | |
description: { | |
type: GraphQLString, | |
resolve: (payload) => payload | |
} | |
}, | |
mutateAndGetPayload: async ({description, name, enabled, tags}) => { | |
let model = new ScopeModel({description: description, name: name , enabled: enabled, tags: tags}); | |
let result = await model.save(); | |
console.log(result); | |
var newScope = { | |
id: id, | |
description: desdescription, | |
name: name, | |
enabled: enabled, | |
name: shipName | |
}; | |
return newScope; | |
} | |
}); | |
let schema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'Query', | |
fields: () => ({ | |
scopes: { | |
type: scopeConnection, | |
args: connectionArgs, | |
resolve: async (scope, args) => connectionFromPromisedArray(ScopeModel.find({}).limit(args.first), args) | |
}, | |
grants: { | |
type: grantConnection, | |
args: connectionArgs, | |
resolve: async (grant, args) => connectionFromArray(await GrantModel.find({}).limit(args.first), args) | |
} | |
}) | |
}), | |
mutation: new GraphQLObjectType({ | |
name: 'Mutation', | |
fields: () => ({ | |
createScope: scopeMutation | |
}) | |
}) | |
}); | |
return schema; | |
} | |
export default Schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment