Created
July 10, 2018 21:01
-
-
Save jmcneese/d2cc35c26138cd9842c73a3171746478 to your computer and use it in GitHub Desktop.
modular graphql schema
This file contains 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
// schema/types/community.js | |
import { GraphQLBoolean, GraphQLObjectType, GraphQLString } from 'graphql'; | |
import { connectionArgs, globalIdField } from 'graphql-relay'; // relay-specific | |
// if you are using relay, you'd define an interface for nodes, and include it here | |
import { nodeInterface } from 'schema/lib/node'; | |
/** | |
* Community type | |
*/ | |
export default new GraphQLObjectType({ | |
name: 'Community', | |
description: 'A community', | |
interfaces: () => [nodeInterface], // relay-specific | |
fields: () => ({ | |
id: globalIdField('communities'), // relay-specific | |
slug: { | |
type: GraphQLString, | |
description: 'The slug of a community', | |
}, | |
name: { | |
type: GraphQLString, | |
description: 'The name of a community', | |
}, | |
description: { | |
type: GraphQLString, | |
description: 'The description of a community', | |
}, | |
featured: { | |
type: GraphQLBoolean, | |
description: 'Whether a community is featured', | |
resolve: () => { | |
/* removed for sensitive business logic */ | |
}, | |
}, | |
liked: { | |
type: GraphQLBoolean, | |
description: 'Whether community is liked by the viewer', | |
resolve: () => { | |
/* removed for sensitive business logic */ | |
}, | |
}, | |
member: { | |
type: GraphQLBoolean, | |
description: 'Whether the current user is a member of a community', | |
resolve: () => { | |
/* removed for sensitive business logic */ | |
}, | |
}, | |
}), | |
}); |
This file contains 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
// schema/mutations/joinCommunity.js | |
import { GraphQLID, GraphQLNonNull } from 'graphql'; | |
import { mutationWithClientMutationId } from 'graphql-relay'; // only if you are using relay, obviously | |
import CommunityType from 'schema/types/community'; | |
import viewer from 'schema/queries/viewer'; | |
/** | |
* JoinCommunity mutation | |
* | |
* Mutation that accepts new data for the viewer's person record, and returns the updated viewer | |
*/ | |
export default mutationWithClientMutationId({ | |
name: 'JoinCommunity', | |
/** | |
* inputFields | |
* | |
* Field definition for JoinCommunityInput | |
*/ | |
inputFields: () => ({ | |
communityId: { | |
type: new GraphQLNonNull(GraphQLID), | |
description: 'The ID of a community', | |
}, | |
}), | |
/** | |
* outputFields | |
* | |
* Field definition for JoinCommunityPayload | |
*/ | |
outputFields: () => ({ | |
community: { | |
type: CommunityType, | |
description: 'The community that was joined', | |
resolve: () => { | |
/* removed for sensitive business logic */ | |
}, | |
}, | |
communityEdge: { | |
type: getConnection(CommunityType).edgeType, | |
description: 'The newly created community edge', | |
resolve: () => { | |
/* removed for sensitive business logic */ | |
}, | |
}, | |
communityId: { | |
type: GraphQLID, | |
description: 'The ID of community that was left', | |
}, | |
viewer, | |
}), | |
/** | |
* mutateAndGetPayload | |
* | |
* Process the input data, and send to api. Return result data as payload. | |
* | |
* @param {string} communityId | |
* @return {Promise} | |
*/ | |
mutateAndGetPayload: ({ communityId }) => { | |
/* do the actual data mutation here */, | |
return { communityId }; | |
}, | |
}); |
This file contains 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
// schema/mutations/index.js | |
import { GraphQLObjectType } from 'graphql'; | |
import joinCommunity from './joinCommunity'; | |
import leaveCommunity from './leaveCommunity'; | |
import likeCommunity from './likeCommunity'; | |
/** | |
* Root mutations | |
*/ | |
export default new GraphQLObjectType({ | |
name: 'Mutations', | |
fields: () => ({ | |
joinCommunity, | |
leaveCommunity, | |
likeCommunity, | |
}), | |
}); |
This file contains 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
// schema/queries/index.js | |
import { GraphQLObjectType } from 'graphql'; | |
import viewer from 'schema/queries/viewer'; | |
/** | |
* Root queries | |
*/ | |
export default new GraphQLObjectType({ | |
name: 'Queries', | |
fields: () => ({ | |
// if you use relay, node and nodes fields go here, otherwise just include all your top-level queries | |
viewer, | |
}), | |
}); |
This file contains 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
// schema/index.js | |
import { GraphQLSchema } from 'graphql'; | |
import mutation from 'schema/mutations'; | |
import query from 'schema/queries'; | |
/** | |
* Root schema | |
*/ | |
export default new GraphQLSchema({ | |
mutation, | |
query, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment