File size: 2,160 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// @flow

const debug = require('debug')('api:utils:error-formatter');

import { graphql, print } from 'graphql';
import { makeExecutableSchema } from 'graphql-tools';

import createErrorFormatter from '../../utils/create-graphql-error-formatter';

const typeDefs = `
	type Community {
		id: ID!
    threadConnection(first: Int = 10, after: String): CommunityThreadsConnection!
	}

	type CommunityThreadsConnection {
		edges: [CommunityThreadEdge!]
	}

	type CommunityThreadEdge {
		node: Thread!
	}

	type Thread {
		id: ID!
		channel: Channel!
	}

	type Channel {
		id: ID!
	}

	type Query {
		community(id: ID): Community
	}
`;

const resolvers = {
  Query: {
    community: () => ({ id: 1 }),
  },
  Community: {
    threadConnection: () => ({}),
  },
  CommunityThreadsConnection: {
    edges: () => [6],
  },
  CommunityThreadEdge: {
    node: id => ({ id }),
  },
  Thread: {
    channel: () => null,
  },
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const query = `
    query getCommunityThreadConnection( $id: ID ){
      community( id: $id ) {
        id
        threadConnection {
          edges {
            node {
              id
              channel {
                id
              }
            }
          }
        }
      }
    }
    `;

const variables = { id: 1 };

describe('createGraphQLErrorFormatter', () => {
  const stderrWrite = process.stderr.write;

  afterEach(() => {
    process.stderr.write = stderrWrite;
  });

  it('returns function', () => {
    expect(createErrorFormatter()).toBeInstanceOf(Function);
  });

  it('logs error path', async () => {
    debug.log = [];

    const result = await graphql(schema, query, null, null, variables);
    const formatter = createErrorFormatter();
    (result.errors || []).forEach(formatter);
    expect(debug.log).toMatchSnapshot();
  });

  it('logs query and variables', async () => {
    debug.log = [];

    const result = await graphql(schema, query, null, null, variables);
    const formatter = createErrorFormatter({ body: { query, variables } });
    (result.errors || []).forEach(formatter);
    expect(debug.log).toMatchSnapshot();
  });
});