github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/integration/integration-test.js (about)

     1  import { createPersistedQueryLink } from "apollo-link-persisted-queries";
     2  import {InMemoryCache} from "apollo-cache-inmemory";
     3  import {HttpLink} from "apollo-link-http";
     4  import { WebSocketLink } from "apollo-link-ws";
     5  import { SubscriptionClient } from "subscriptions-transport-ws";
     6  import ws from 'ws';
     7  import {ApolloClient} from "apollo-client";
     8  import fetch from "node-fetch";
     9  import gql from 'graphql-tag';
    10  
    11  var uri = process.env.SERVER_URL || 'http://localhost:8080/query';
    12  
    13  function test(client) {
    14      describe('Json', () => {
    15          it('should follow json escaping rules', async () => {
    16              let res = await client.query({
    17                  query: gql`{ jsonEncoding }`,
    18              });
    19  
    20              expect(res.data.jsonEncoding).toBe("σΎ“­");
    21              expect(res.errors).toBe(undefined);
    22          });
    23      });
    24  
    25      describe('Input defaults', () => {
    26          it('should pass default values to resolver', async () => {
    27              let res = await client.query({
    28                  query: gql`{ date(filter:{value: "asdf"}) }`,
    29              });
    30  
    31              expect(res.data.date).toBe(true);
    32              expect(res.errors).toBe(undefined);
    33          });
    34      });
    35  
    36      describe('Complexity', () => {
    37          it('should fail when complexity is too high', async () => {
    38              let res = await client.query({
    39                  query: gql`{ complexity(value: 2000) }`,
    40              });
    41  
    42              expect(res.errors[0].message).toBe("operation has complexity 2000, which exceeds the limit of 1000");
    43          });
    44  
    45          it('should succeed when complexity is not too high', async () => {
    46              let res = await client.query({
    47                  query: gql`{ complexity(value: 100) }`,
    48              });
    49  
    50              expect(res.data.complexity).toBe(true);
    51              expect(res.errors).toBe(undefined);
    52          });
    53      });
    54  
    55      describe('Errors', () => {
    56          it('should respond with correct paths', async () => {
    57              let res = await client.query({
    58                  query: gql`{ path { cc:child { error } } }`,
    59              });
    60  
    61              expect(res.errors[0].path).toEqual(['path', 0, 'cc', 'error']);
    62              expect(res.errors[1].path).toEqual(['path', 1, 'cc', 'error']);
    63              expect(res.errors[2].path).toEqual(['path', 2, 'cc', 'error']);
    64              expect(res.errors[3].path).toEqual(['path', 3, 'cc', 'error']);
    65          });
    66  
    67          it('should use the error presenter for custom errors', async () => {
    68              let res = await client.query({
    69                  query: gql`{ error(type: CUSTOM) }`,
    70              });
    71  
    72              expect(res.errors[0].message).toEqual('User message');
    73          });
    74  
    75          it('should pass through for other errors', async () => {
    76              let res = await client.query({
    77                  query: gql`{ error(type: NORMAL) }`,
    78              });
    79  
    80              expect(res.errors[0].message).toEqual('normal error');
    81          });
    82      });
    83  }
    84  
    85  describe('HTTP client', () => {
    86      const client = new ApolloClient({
    87          link: createPersistedQueryLink().concat(new HttpLink({uri, fetch})),
    88          cache: new InMemoryCache(),
    89          defaultOptions: {
    90              watchQuery: {
    91                  fetchPolicy: 'network-only',
    92                  errorPolicy: 'ignore',
    93              },
    94              query: {
    95                  fetchPolicy: 'network-only',
    96                  errorPolicy: 'all',
    97              },
    98          },
    99      });
   100  
   101     test(client);
   102  });
   103  
   104  describe('Websocket client', () => {
   105      const sc = new SubscriptionClient(uri.replace('http://', 'ws://').replace('https://', 'wss://'), {
   106          reconnect: false,
   107          timeout: 1000,
   108          inactivityTimeout: 100,
   109      }, ws);
   110  
   111      const client = new ApolloClient({
   112              link: createPersistedQueryLink().concat(new WebSocketLink(sc)),
   113              cache: new InMemoryCache(),
   114              defaultOptions: {
   115                  watchQuery: {
   116                      fetchPolicy: 'network-only',
   117                      errorPolicy: 'ignore',
   118                  },
   119                  query: {
   120                      fetchPolicy: 'network-only',
   121                      errorPolicy: 'all',
   122                  },
   123              },
   124          });
   125  
   126      test(client);
   127  
   128      afterAll(() => {
   129          client.stop();
   130          sc.close(true);
   131      });
   132  });