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 { schema } from '../my-schema'; | |
| // Docs for `graphql` function | |
| // http://graphql.org/graphql-js/graphql/#graphql | |
| import { graphql } from 'graphql'; | |
| it('executes a query that looks up some data', async () => { | |
| // optionally create some fake context (i.e. data connectors) | |
| const myFakeContext = { restConnectionOne: { findOne: jest.fn() } }; |
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
| const { ApolloServer } = require('apollo-server'); | |
| const server = new ApolloServer({ | |
| typeDefs, | |
| resolvers, | |
| context: ({ req }) => { | |
| // get the user token from the headers | |
| const token = req.headers.authorization || ''; | |
| // try to retrieve a user with the token |
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
| users: (root, args, context) => { | |
| if (!context.user) return null; | |
| return ['bob', 'jake']; | |
| } |
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
| users: (root, args, context) => { | |
| if (!context.user || !context.user.roles.includes('admin')) return null; | |
| return context.models.User.getAll(); | |
| } |
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
| export const User = { | |
| getAll: () => { /* fetching/transform logic for all users */ }, | |
| getById: (id) => { /* fetching/transform logic for a single user */ }, | |
| getByGroupId: (id) => { /* fetching/transform logic for a group of users */ }, | |
| }; |
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
| type Query { | |
| user (id: ID!): User | |
| article (id: ID!): Article | |
| } | |
| type Article { | |
| author: User | |
| } | |
| type User { |
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
| context: ({ req }) => { | |
| // get the user token from the headers | |
| const token = req.headers.authentication || ''; | |
| // try to retrieve a user with the token | |
| const user = getUser(token); | |
| // optionally block the user | |
| // we could also check user roles/permissions here | |
| if (!user) throw new AuthenticationError('you must be logged in to query this schema'); |
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
| export const generateUserModel = ({ user }) => ({ | |
| getAll: () => { /* fetching/transform logic for all users */ }, | |
| getById: (id) => { /* fetching/transform logic for a single user */ }, | |
| getByGroupId: (id) => { /* fetching/transform logic for a group of users */ }, | |
| }); |
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
| getAll: () => { | |
| if(!user || !user.roles.includes('admin')) return null; | |
| return fetch('http://myurl.com/users'); | |
| } |
OlderNewer