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
it('should render loading state initially', () => { | |
const component = renderer.create( | |
<MockedProvider mocks={[]}> | |
<Dog /> | |
</MockedProvider>, | |
); | |
const tree = component.toJSON(); | |
expect(tree.children).toContain('Loading...'); | |
}); |
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
// dog.test.js | |
import { MockedProvider } from 'react-apollo/test-utils'; | |
// The component AND the query need to be exported | |
import { GET_DOG_QUERY, Dog } from './dog'; | |
const mocks = [ | |
{ | |
request: { |
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
// Not predictable | |
it('renders without error', () => { | |
renderer.create( | |
<ApolloProvider client={client}> | |
<Dog name="Buck" /> | |
</ApolloProvider>, | |
); | |
}); |
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
// Broken because it's missing Apollo Client in the context | |
it('should render without error', () => { | |
renderer.create(<Dog name="Buck" />); | |
}); |
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
import React from 'react'; | |
import gql from 'graphql-tag'; | |
import { Query } from 'react-apollo'; | |
// Make sure the query is also exported -- not just the component | |
export const GET_DOG_QUERY = gql` | |
query getDog($name: String) { | |
dog(name: $name) { | |
id | |
name |
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
// src/server.js | |
context: ({ req }) => { | |
// pass the request information through to the model | |
return { | |
user, | |
models: { | |
User: generateUserModel({ req }), | |
... | |
} | |
}; |
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
getAll: () => { | |
if(!user || !user.roles.includes('admin')) return null; | |
return fetch('http://myurl.com/users'); | |
} |
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
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 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 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 { |