github.com/kerryoscer/gqlgen@v0.17.29/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('List Coercion', () => {
    56  
    57          it('should succeed when nested single values are passed', async () => {
    58              const variable = {
    59                  enumVal: "CUSTOM",
    60                  strVal: "test",
    61                  intVal: 1,
    62              }
    63              let res = await client.query({
    64                  variables: {in: variable},
    65                  query: gql`query coercion($in: [ListCoercion!]){ coercion(value: $in ) }`,
    66              });
    67  
    68              expect(res.data.coercion).toBe(true);
    69          });
    70  
    71          it('should succeed when single value is passed', async () => {
    72              let res = await client.query({
    73                  query: gql`{ coercion(value: [{
    74                      enumVal: CUSTOM
    75                  }]) }`,
    76              });
    77  
    78              expect(res.data.coercion).toBe(true);
    79          });
    80  
    81          it('should succeed when single scalar value is passed', async () => {
    82              let res = await client.query({
    83                  query: gql`{ coercion(value: [{
    84                      scalarVal: {
    85                          key : someValue
    86                      }
    87                  }]) }`,
    88              });
    89  
    90              expect(res.data.coercion).toBe(true);
    91          });
    92  
    93          it('should succeed when multiple values are passed', async () => {
    94              let res = await client.query({
    95                  query: gql`{ coercion(value: [{
    96                      enumVal: [CUSTOM,NORMAL]
    97                  }]) }`,
    98              });
    99  
   100              expect(res.data.coercion).toBe(true);
   101          });
   102  
   103      });
   104  
   105      describe('Errors', () => {
   106          it('should respond with correct paths', async () => {
   107              let res = await client.query({
   108                  query: gql`{ path { cc:child { error } } }`,
   109              });
   110  
   111              expect(res.errors[0].path).toEqual(['path', 0, 'cc', 'error']);
   112              expect(res.errors[1].path).toEqual(['path', 1, 'cc', 'error']);
   113              expect(res.errors[2].path).toEqual(['path', 2, 'cc', 'error']);
   114              expect(res.errors[3].path).toEqual(['path', 3, 'cc', 'error']);
   115          });
   116  
   117          it('should use the error presenter for custom errors', async () => {
   118              let res = await client.query({
   119                  query: gql`{ error(type: CUSTOM) }`,
   120              });
   121  
   122              expect(res.errors[0].message).toEqual('User message');
   123          });
   124  
   125          it('should pass through for other errors', async () => {
   126              let res = await client.query({
   127                  query: gql`{ error(type: NORMAL) }`,
   128              });
   129  
   130              expect(res.errors[0].message).toEqual('normal error');
   131          });
   132      });
   133  }
   134  
   135  describe('HTTP client', () => {
   136      const client = new ApolloClient({
   137          link: createPersistedQueryLink().concat(new HttpLink({uri, fetch})),
   138          cache: new InMemoryCache(),
   139          defaultOptions: {
   140              watchQuery: {
   141                  fetchPolicy: 'network-only',
   142                  errorPolicy: 'ignore',
   143              },
   144              query: {
   145                  fetchPolicy: 'network-only',
   146                  errorPolicy: 'all',
   147              },
   148          },
   149      });
   150  
   151     test(client);
   152  });
   153  
   154  describe('Websocket client', () => {
   155      const sc = new SubscriptionClient(uri.replace('http://', 'ws://').replace('https://', 'wss://'), {
   156          reconnect: false,
   157          timeout: 1000,
   158          inactivityTimeout: 100,
   159      }, ws);
   160  
   161      const client = new ApolloClient({
   162              link: createPersistedQueryLink().concat(new WebSocketLink(sc)),
   163              cache: new InMemoryCache(),
   164              defaultOptions: {
   165                  watchQuery: {
   166                      fetchPolicy: 'network-only',
   167                      errorPolicy: 'ignore',
   168                  },
   169                  query: {
   170                      fetchPolicy: 'network-only',
   171                      errorPolicy: 'all',
   172                  },
   173              },
   174          });
   175  
   176      test(client);
   177  
   178      afterAll(() => {
   179          client.stop();
   180          sc.close(true);
   181      });
   182  });