Skip to content

Commit

Permalink
chore(test): make the gql api/client a fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 17, 2023
1 parent f09334f commit 76a6244
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
59 changes: 59 additions & 0 deletions examples/react/src/graphqlApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";
// Using the example graphql api here: https://www.apollographql.com/blog/graphql/examples/building-a-graphql-api/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const server_1 = require("@apollo/server");
const graphql_tag_1 = __importDefault(require("graphql-tag"));
const { startStandaloneServer } = require('@apollo/server/standalone');
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = (0, graphql_tag_1.default) `
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields: 'title' and 'author'.
type Book {
title: String
author: String
}
# The "GetBooksQuery" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "GetBooksQuery" query returns an array of zero or more Books (defined above).
type Query {
books: [Book]
}
`;
const books = [
{
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
},
{
title: 'Wuthering Heights',
author: 'Emily Brontë',
},
];
// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
},
};
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new server_1.ApolloServer({ typeDefs, resolvers });
startStandaloneServer(server, {
listen: {
port: 4000,
path: '/graphql',
},
//@ts-ignore
}).then((url) => {
console.log(`🚀 Server ready at ${url.url}`);
});
36 changes: 36 additions & 0 deletions examples/react/src/graphqlClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetBooksQuery = void 0;
const client_1 = require("@apollo/client");
const graphql_tag_1 = __importDefault(require("graphql-tag"));
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const client = new client_1.ApolloClient({
cache: new client_1.InMemoryCache(),
link: new client_1.HttpLink({ uri: 'http://127.0.0.1:4000/graphql', fetch: cross_fetch_1.default }),
headers: {
foo: 'bar',
},
});
function GetBooksQuery() {
return client
.query({
query: (0, graphql_tag_1.default) `
query GetBooksQuery {
books {
title
author
}
}
`,
})
//@ts-ignore
.then((result) => result.data);
}
exports.GetBooksQuery = GetBooksQuery;
// For testing
// GetBooksQuery().then(results => {
// console.log(results);
// })
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pactFromMswServerGql.msw.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetBooksQuery } from '../examples/react/src/graphqlClient';
import { GetBooksQuery } from './fixtures/graphqlClient';
import { graphql } from "msw";
import { setupServer } from "msw/node";
import { setupPactMswAdapter, PactFile } from "./pactMswAdapter";
Expand Down

0 comments on commit 76a6244

Please sign in to comment.