Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get tests to work with fragments #8

Open
tzimmermann opened this issue May 28, 2018 · 3 comments
Open

Cannot get tests to work with fragments #8

tzimmermann opened this issue May 28, 2018 · 3 comments

Comments

@tzimmermann
Copy link

I try to run my GraphQL queries against a mocked schema, and it runs fine, except for any queries that contain a fragment. For these, I keep getting this error:

Unknown fragment "SomeFragmentName".

My test setup:

  • I have the transform set in the jest.config.js exactly as described in this project's README.
  • In my test file, I generate a schema using makeExecutableSchema and addMockFunctionsToSchema.
  • Then, I run the queries like this:
const execute = ({ query, done, variables = {} }) => {
    let queryString = query.loc.source.body


    const promise = graphql(
        //  schema
        schema,
        queryString,
        null,
        null,
        variables
    )

    return promise.then(({ data, errors }) => {
        if (errors) {
            return done.fail(`Errors in query:\n${errors.join('\n')}`)
        }
        expect(data).toMatchSnapshot()
        return Promise.resolve()
    })
}
  • An example query that fails to run looks like this:
#import '../../fragments/Widget.graphql'

query WidgetFragment($id: ID) {
    readWidgetFragment(id: $id) {
        ...Widget
    }
}

Maybe I'm overlooking something here, would be grateful for any help!

@realalexhomer
Copy link

Has anyone figured out a way to get around this?

@jmadson
Copy link

jmadson commented Jul 2, 2019

Any luck with this issue?

@OscarBarrett
Copy link

query.loc.source.body does not include the contents of the imported Fragment, only your actual query. The imported fragment is inquery.definitions.

If your client must take a string, then you can use graphql/language/printer to print out the complete query.

import { print } from 'graphql/language/printer';

graphql(schema, print(query), ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants