Skip to content

Commit 4963722

Browse files
fix issues with AS4 generated-types example (#64)
* The query and mutation types were nested incorrectly. Everything was typed as `any`. * The data source recreated the fixtures with every request so the mutation had no effect
1 parent 2074ba9 commit 4963722

File tree

5 files changed

+49
-49
lines changed

5 files changed

+49
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
// Use our automatically generated Book and AddBookMutationResponse types
22
// for type safety in our data source class
3-
import { AddBookMutationResponse, Book } from './__generated__/resolvers-types';
3+
import { AddBookMutationResponse, Book } from "./__generated__/resolvers-types";
44

5-
export class BooksDataSource {
6-
// Our example static data set
7-
books: { title?: string; author?: string }[] = [
8-
{
9-
title: 'The Awakening',
10-
author: 'Kate Chopin',
11-
},
12-
{
13-
title: 'City of Glass',
14-
author: 'Paul Auster',
15-
},
16-
];
5+
const BooksDB: Omit<Required<Book>, "__typename">[] = [
6+
{
7+
title: "The Awakening",
8+
author: "Kate Chopin",
9+
},
10+
{
11+
title: "City of Glass",
12+
author: "Paul Auster",
13+
},
14+
];
1715

16+
export class BooksDataSource {
1817
getBooks(): Book[] {
1918
// simulate fetching a list of books
20-
return this.books;
19+
return BooksDB;
2120
}
2221

2322
// We are using a static data set for this small example, but normally
2423
// this Mutation would *mutate* our underlying data using a database
2524
// or a REST API.
2625
async addBook(book: Book): Promise<AddBookMutationResponse> {
27-
this.books.push(book);
28-
console.log(this.books);
26+
if (book.title && book.author) {
27+
BooksDB.push({ title: book.title, author: book.author });
2928

30-
return {
31-
code: '200',
32-
success: true,
33-
message: 'New book added!',
34-
book: this.books[this.books.length - 1],
35-
};
29+
return {
30+
code: "200",
31+
success: true,
32+
message: "New book added!",
33+
book,
34+
};
35+
} else {
36+
return {
37+
code: "400",
38+
success: false,
39+
message: "Invalid input",
40+
book: null,
41+
};
42+
}
3643
}
3744
}

apollo-server/v4/generated-types/src/index.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { ApolloServer } from '@apollo/server';
2-
import { startStandaloneServer } from '@apollo/server/standalone';
3-
import { BooksDataSource } from './datasources.js';
4-
// Here we import the automatically generated Book type, so we can use it in our
5-
// context typing.
6-
import { Book } from './__generated__/resolvers-types';
7-
import resolvers from './resolvers/index.js';
8-
import { readFileSync } from 'fs';
1+
import { ApolloServer } from "@apollo/server";
2+
import { startStandaloneServer } from "@apollo/server/standalone";
3+
import { BooksDataSource } from "./datasources.js";
4+
import resolvers from "./resolvers/index.js";
5+
import { readFileSync } from "fs";
96

107
// Note: this only works locally because it relies on `npm` routing
118
// from the root directory of the project.
12-
const typeDefs = readFileSync('./schema.graphql', { encoding: 'utf-8' });
9+
const typeDefs = readFileSync("./schema.graphql", { encoding: "utf-8" });
1310

1411
export interface MyContext {
1512
dataSources: {
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Resolvers } from '../__generated__/resolvers-types';
2-
import queries from './queries.js';
3-
import mutations from './mutations.js';
1+
import { Resolvers } from "../__generated__/resolvers-types";
2+
import Query from "./queries.js";
3+
import Mutation from "./mutations.js";
44

55
// Note this "Resolvers" type isn't strictly necessary because we are already
66
// separately type checking our queries and resolvers. However, the "Resolvers"
77
// generated types is useful syntax if you are defining your resolvers
88
// in a single file.
9-
const resolvers: Resolvers = { ...queries, ...mutations };
9+
const resolvers: Resolvers = { Query, Mutation };
1010

1111
export default resolvers;

apollo-server/v4/generated-types/src/resolvers/mutations.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { MutationResolvers } from '__generated__/resolvers-types';
1+
import { MutationResolvers } from "__generated__/resolvers-types";
22

33
// Use the generated `MutationResolvers` type to type check our mutations!
44
const mutations: MutationResolvers = {
5-
Mutation: {
6-
// Below, we mock adding a new book. Our data set is static for this
7-
// example, so we won't actually modify our data.
8-
addBook: async (_, { title, author }, { dataSources }) => {
9-
return await dataSources.booksAPI.addBook({ title, author });
10-
},
5+
// Below, we mock adding a new book. Our data set is static for this
6+
// example, so we won't actually modify our data.
7+
addBook: async (_, { title, author }, { dataSources }) => {
8+
return dataSources.booksAPI.addBook({ title, author });
119
},
1210
};
1311

apollo-server/v4/generated-types/src/resolvers/queries.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { QueryResolvers } from '__generated__/resolvers-types';
1+
import { QueryResolvers } from "__generated__/resolvers-types";
22

33
// Use the generated `QueryResolvers` type to type check our queries!
44
const queries: QueryResolvers = {
5-
Query: {
6-
// Our third argument (`contextValue`) has a type here, so we
7-
// can check the properties within our resolver's shared context value.
8-
books: async (_, __, contextValue) => {
9-
return await contextValue.dataSources.booksAPI.getBooks();
10-
},
5+
// Our third argument (`contextValue`) has a type here, so we
6+
// can check the properties within our resolver's shared context value.
7+
books: async (_, __, { dataSources }) => {
8+
return dataSources.booksAPI.getBooks();
119
},
1210
};
1311

0 commit comments

Comments
 (0)