Skip to content

Instantly share code, notes, and snippets.

@bengrunfeld
bengrunfeld / possibleTypes.ts
Created August 14, 2023 14:09
Apollo Fragments - possibleTypes
var possibleTypes = schema.data.__schema.types
.filter(supertype => supertype.possibleTypes)
.reduce((all, supertype) => {
all[supertype.name] = supertype.possibleTypes.map(subtype => subtype.name);
return all;
}, {});
@bengrunfeld
bengrunfeld / types.js
Created June 2, 2021 15:46
Scalable GraphQL Types
export const types = `
type User {
id: ID
email: String
password: String
loggedIn: Boolean
firstName: String
lastName: String
}
`;
@bengrunfeld
bengrunfeld / resolvers.js
Created June 2, 2021 15:41
Scalable GraphQL User Resolvers
const queries = {
user: (root, args) => {
return {
id: "12345",
email: "some.user@email.com",
password: "Pa$$w0rd!",
loggedIn: false,
firstName: "Some",
lastName: "User",
};
@bengrunfeld
bengrunfeld / queries.js
Created June 2, 2021 14:48
Scalable GraphQL User Queries File
export const queries = `
user(id: String!) : User
`;
@bengrunfeld
bengrunfeld / mutations.js
Created June 2, 2021 14:46
Scalable GraphQL User Mutations File
export const mutations = `
createUser(
email: String!
password: String!
firstName: String!
lastName: String!
): User
`;
@bengrunfeld
bengrunfeld / index.js
Created June 2, 2021 14:44
Scalable GraphQL User Index File
import { queries } from "./queries.js"
import { mutations } from "./mutations.js"
import { resolvers } from "./resolvers.js"
import { types } from "./types"
export const User = { queries, mutations, resolvers, types }
@bengrunfeld
bengrunfeld / typeDefs.js
Created June 2, 2021 14:29
GraphQL Scalable TypeDefs File
import { gql } from "apollo-server-express";
import { User } from "./User";
const typeDefs = gql`
${User.types}
type Query {
${User.queries}
}
@bengrunfeld
bengrunfeld / resolvers.js
Created June 2, 2021 14:25
GraphQL Scalable Resolvers File
import { User } from './User'
const resolvers = {
Query: {
...User.resolvers.queries,
},
Mutation: {
...User.resolvers.mutations,
}
};
@bengrunfeld
bengrunfeld / index.js
Created June 2, 2021 14:20
GraphQL Index File
export { default as typeDefs } from './typeDefs'
export { default as resolvers } from './resolvers'
@bengrunfeld
bengrunfeld / graphql-server-express.js
Last active June 2, 2021 13:51
GraphQL Express Server
import express from 'express'
import { ApolloServer } from 'apollo-server-express'
import { typeDefs, resolvers } from '../graphql'
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true
})