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

[GraphQL] Add support for mutations #565

Closed
1 task done
adrianwilk89 opened this issue Jan 4, 2021 · 7 comments
Closed
1 task done

[GraphQL] Add support for mutations #565

adrianwilk89 opened this issue Jan 4, 2021 · 7 comments

Comments

@adrianwilk89
Copy link

Issue Classification

  • Feature request

Feature Request

Currently it's not possible to test mutation. To GraphQLInteraction class should be added "withMutation" method.

@TimothyJones
Copy link
Contributor

Could you elaborate a bit on why this isn't possible? I'm not too familiar with GraphQL terms, but a quick look at the GraphQL structure suggests that you should be able to do this with appropriate values to withOperation and withQuery.

It is definitely the case that our GraphQL support could be improved, though.

@TimothyJones
Copy link
Contributor

The GraphQL documentation has the following example:

var author = 'andy';
var content = 'hope is a good thing';
var query = `mutation CreateMessage($input: MessageInput) {
  createMessage(input: $input) {
    id
  }
}`;
 
fetch('/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query,
    variables: {
      input: {
        author,
        content,
      }
    }
  })
})
  .then(r => r.json())
  .then(data => console.log('data returned:', data));

This suggests that it would be appropriate to use withQuery with a mutation, and it should just work.

If this doesn't work for you, it would be helpful to include the error messages you are getting, and ideally a modified version of the graphQL example that reproduces the problem.

@adrianwilk89
Copy link
Author

adrianwilk89 commented Jan 5, 2021

@TimothyJones if I use withQuery for mutation im getting error like "no interaction found on POST"

this is my pseudo code setup for mutation

const client = new ApolloClient({
    cache: new InMemoryCache({
        addTypename: false
    }),
    link: createHttpLink({
        fetch: require('node-fetch'),
        headers: {
            'content-type': 'application/json'
        },
        uri: 'http://localhost:4012/1746213/v1/',
    })
});

export function mutation() {
    return client
        .mutate({
            mutation: GQLTag(setTest)
        }).then((result: any) => result.data);
}

and this is my pseudo code setup for spec

const provider = new Pact({
    port: 4012,
    log: path.resolve(process.cwd(), 'logs', 'set-test-integration.log'),
    spec: 2,
    dir: path.resolve(process.cwd(), 'pacts/set-test'),
    consumer: 'test-panel',
    provider: 'test-api'
});

test.before('setting up graphql', async () => {
    await provider.setup();
});

test('Deal', async (t: any) => {
    t.plan(1);

    const graphqlMutation = new GraphQLInteraction()
        .withQuery(setTest)
        .uponReceiving('test')
        .withOperation('setTest')
        .withVariables({})
        .withRequest({
            path: '/1746213/v1/',
            method: 'POST',
        })
        .willRespondWith({
            status: 200,
            headers: {
                'Content-Type': 'application/json',
            },
            body: {
                data: like(data)
            }
        });

    await provider.addInteraction(graphqlMutation);

    const response = await mutation();
    
    t.deepEqual(response.data, data.data);

});

test.afterEach(async (t: any) => {
    await t.notThrowsAsync(provider.verify());
});

test.after('pact.js mock server graceful shutdown', async () => {
    await provider.finalize();
});

@TimothyJones
Copy link
Contributor

TimothyJones commented Jan 5, 2021 via email

@adrianwilk89
Copy link
Author

adrianwilk89 commented Jan 5, 2021

@TimothyJones I realized that the error code in the console was confusing because I found information about mismatched strings in the log file.

 {
   "body": {
-    "query": "mutation setTest($deal:DASDealInput){dream_adserver{set_deal (deal: $deal){hit{deal_id deal_name}}}}\n"
+    "query": "mutation setTest($deal: DASDealInput) {\n  dream_adserver {\n    set_deal(deal: $deal) {\n      hit {\n        deal_id\n        deal_name\n      }\n    }\n  }\n}\n"
   }
 }

@adrianwilk89
Copy link
Author

adrianwilk89 commented Jan 5, 2021

@TimothyJones i solved the problem by using spaces instead tabulators (1tab=2spaces) and by removing spaces between methods and opening brace

this is wrong version

mutation setTest ($deal: DASDealInput){
    dream_adserver {
        set_deal (deal: $deal) {
            hit {
                deal_id
                deal_name
            }
        }
    }
}

and this is working version

mutation setTest($deal: DASDealInput){
  dream_adserver {
    set_deal(deal: $deal) {
      hit {
        deal_id
        deal_name
      }
    }
  }
}

is possible to change matching behavior ????

@TimothyJones
Copy link
Contributor

Ah, yes. This sounds like #292, which we'd like to fix at some point. I'll close this, as we are already tracking the whitespace issue over there.

I've opened #572 to track improving the error message you mentioned. Thanks for the report!

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

2 participants