Skip to content

Commit

Permalink
Step 4.3: Add chat field to Query type
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed May 20, 2020
1 parent 5ab032e commit 310c97f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions schema/resolvers.ts
Expand Up @@ -21,6 +21,10 @@ const resolvers = {
chats() {
return chats;
},

chat(root: any, { chatId }: any) {
return chats.find(c => c.id === chatId);
},
},
};

Expand Down
1 change: 1 addition & 0 deletions schema/typeDefs.graphql
Expand Up @@ -17,4 +17,5 @@ type Chat {

type Query {
chats: [Chat!]!
chat(chatId: ID!): Chat
}
16 changes: 16 additions & 0 deletions tests/queries/__snapshots__/getChat.test.ts.snap
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Query.chat should fetch specified chat 1`] = `
Object {
"chat": Object {
"id": "1",
"lastMessage": Object {
"content": "You on your way?",
"createdAt": "2018-12-31T07:20:00.000Z",
"id": "1",
},
"name": "Ethan Gonzalez",
"picture": "https://randomuser.me/api/portraits/thumb/men/1.jpg",
},
}
`;
33 changes: 33 additions & 0 deletions tests/queries/getChat.test.ts
@@ -0,0 +1,33 @@
import { createTestClient } from 'apollo-server-testing';
import { ApolloServer, gql } from 'apollo-server-express';
import schema from '../../schema';

describe('Query.chat', () => {
it('should fetch specified chat', async () => {
const server = new ApolloServer({ schema });

const { query } = createTestClient(server);

const res = await query({
variables: { chatId: '1' },
query: gql`
query GetChat($chatId: ID!) {
chat(chatId: $chatId) {
id
name
picture
lastMessage {
id
content
createdAt
}
}
}
`,
});

expect(res.data).toBeDefined();
expect(res.errors).toBeUndefined();
expect(res.data).toMatchSnapshot();
});
});
27 changes: 27 additions & 0 deletions types/apollo-server-testing.d.ts
@@ -0,0 +1,27 @@
declare module 'apollo-server-testing' {
import { ApolloServerBase } from 'apollo-server-core';
import { print, DocumentNode } from 'graphql';
import { GraphQLResponse } from 'graphql-extensions';

type StringOrAst = string | DocumentNode;

// A query must not come with a mutation (and vice versa).
type Query<TVariables> = {
query: StringOrAst;
mutation?: undefined;
variables?: TVariables;
};

type Mutation<TVariables> = {
mutation: StringOrAst;
query?: undefined;
variables?: TVariables;
};

export const createTestClient: <TVariables>(
server: ApolloServerBase
) => {
query: (query: Query<TVariables>) => Promise<GraphQLResponse>;
mutate: (mutation: Mutation<TVariables>) => Promise<GraphQLResponse>;
};
}

0 comments on commit 310c97f

Please sign in to comment.