github.com/fortexxx/gqlgen@v0.10.3-0.20191216030626-ca5ea8b21ead/integration/integration-test.js (about)

     1  import {InMemoryCache} from "apollo-cache-inmemory";
     2  import {HttpLink} from "apollo-link-http";
     3  import {ApolloClient} from "apollo-client";
     4  import fetch from "node-fetch";
     5  import gql from 'graphql-tag';
     6  
     7  var uri = process.env.SERVER_URL || 'http://localhost:8080/query';
     8  
     9  const client = new ApolloClient({
    10      link: new HttpLink({uri, fetch}),
    11      cache: new InMemoryCache(),
    12      defaultOptions: {
    13          watchQuery: {
    14              fetchPolicy: 'network-only',
    15              errorPolicy: 'ignore',
    16          },
    17          query: {
    18              fetchPolicy: 'network-only',
    19              errorPolicy: 'all',
    20          },
    21      },
    22  });
    23  
    24  describe('Json', () => {
    25      it('should follow json escaping rules', async () => {
    26          let res = await client.query({
    27              query: gql`{ jsonEncoding }`,
    28          });
    29  
    30          expect(res.data.jsonEncoding).toBe("σΎ“­");
    31          expect(res.errors).toBe(undefined);
    32      });
    33  });
    34  
    35  describe('Input defaults', () => {
    36      it('should pass default values to resolver', async () => {
    37          let res = await client.query({
    38              query: gql`{ date(filter:{value: "asdf"}) }`,
    39          });
    40  
    41          expect(res.data.date).toBe(true);
    42          expect(res.errors).toBe(undefined);
    43      });
    44  });
    45  
    46  describe('Complexity', () => {
    47      it('should fail when complexity is too high', async () => {
    48          let res;
    49          try {
    50              res = await client.query({
    51                  query: gql`{ complexity(value: 2000) }`,
    52              });
    53          } catch (err) {
    54              expect(err.networkError.statusCode).toEqual(422);
    55          }
    56          expect(res).toBe(undefined);
    57      });
    58  
    59      it('should succeed when complexity is not too high', async () => {
    60          let res = await client.query({
    61              query: gql`{ complexity(value: 100) }`,
    62          });
    63  
    64          expect(res.data.complexity).toBe(true);
    65          expect(res.errors).toBe(undefined);
    66      });
    67  });
    68  
    69  describe('Errors', () => {
    70      it('should respond with correct paths', async () => {
    71          let res = await client.query({
    72              query: gql`{ path { cc:child { error } } }`,
    73          });
    74  
    75          expect(res.errors[0].path).toEqual(['path', 0, 'cc', 'error']);
    76          expect(res.errors[1].path).toEqual(['path', 1, 'cc', 'error']);
    77          expect(res.errors[2].path).toEqual(['path', 2, 'cc', 'error']);
    78          expect(res.errors[3].path).toEqual(['path', 3, 'cc', 'error']);
    79      });
    80  
    81      it('should use the error presenter for custom errors', async () => {
    82          let res = await client.query({
    83              query: gql`{ error(type: CUSTOM) }`,
    84          });
    85  
    86          expect(res.errors[0].message).toEqual('User message');
    87      });
    88  
    89      it('should pass through for other errors', async () => {
    90          let res = await client.query({
    91              query: gql`{ error(type: NORMAL) }`,
    92          });
    93  
    94          expect(res.errors[0].message).toEqual('normal error');
    95      });
    96  });