Mayank1791989 / gql

112 stars 19 forks source link

"There can only be one fragment named Xyz" (UniqueFragmentNames) #170

Open borekb opened 4 years ago

borekb commented 4 years ago

I have two tests like this:

import { gql } from 'apollo-server';
import { createTestClient } from 'apollo-server-testing';
import { apolloServer } from './apolloServer';

it('test 1', async () => {
  const QUERY = gql`
    {
      example {
        ...MyFragment
      }
    }

    fragment MyFragment on Character {
      name
      age
    }
  `;
  const { query } = createTestClient(apolloServer);
  const result = await query({
    query: QUERY,
  });
  expect(result).toMatchSnapshot();
});

it('test 2', async () => {
  const QUERY = gql`
    {
      example2 {
        ...MyFragment
      }
    }

    fragment MyFragment on Character {
      name
      age
    }
  `;
  const { query } = createTestClient(apolloServer);
  const result = await query({
    query: QUERY,
  });
  expect(result).toMatchSnapshot();
});

Both run fine at runtime but at development time (in VSCode via Kumar's extension), I get this error:

Screenshot 2019-10-01 at 10 34 47

There can be only one fragment named "MyFragment". (UniqueFragmentNames)

It looks like the validation isn't scoped to a single gql` ` string or something.

This is my config:

{
  "schema": {
    "files": "graphql-server/src/**/*.graphql"
  },
  "query": {
    "files": [
      {
        "match": ["graphql-server/src/**/*.test.ts"],
        "parser": ["EmbeddedQueryParser", { "startTag": "gql`", "endTag": "`" }],
      },
    ]
  }
}
borekb commented 4 years ago

By the way, using the same query names does this too (e.g., having query ExampleQuery twice).

Mayank1791989 commented 4 years ago

@borekb Currently scope is file. In V3 branch I have made scope configurable. For now you can suppress errors in your test files by disabling the validation rule.