question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UpdateQueries with inline fragments fails

See original GitHub issue

I have a query that looks something like this:

query getGraph {
  nodes {
    ...NodeContent
  }
}
${nodeFragment}

where nodeFragment is:

fragment NodeContent on Node {
  id
  type
  x
  y
  ... on TriggerNode {
    properties {
      trigger
    }
  }
  ... on ReplyNode {
    properties {
      reply
      keep
    }
  }
}

This runs fine, and I get back the desired result.

I then want to perform a mutation that adds a node, and therefore runs updateQueries. So I wrote a query like this:

mutation createNode ($type:String!, $x:Int!, $y:Int!, $properties:PropertiesInput!) {
  createNode(type:$type, x:$x, y:$y, properties:$properties) {
    ...NodeContent
  }
}
${nodeFragment}

And called it like this:

const withCreateNodeMutation = graphql(createNodeMutation, {
  props: ({ mutate }) => ({
    createNode: variables => mutate({
      variables,
      updateQueries: {
        getGraph: () => { console.log('hello world'); },
      },
    }),
  }),
});

Intended outcome:

I expected the client to at least run the updateQueries function and print hello world in the console.

Actual outcome:

Nothing happens!

Interestingly, I am getting back the correct, complete result in the APOLLO_MUTATION_RESULT action (with the correct __typenames on the fragments and everything). The updateQueries function is just not called.

This also makes optimistic UI impossible.

Could be related to #1363, #1379?

Would appreciate any thoughts or workarounds - perhaps the new imperative API? Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
bensalilijamescommented, Apr 2, 2017

@Mike-Dax I upgraded to react-apollo@1.0.0 (and therefore apollo-client@1.0.1), and also passed my schema into fragmentMatcher on the apollo client options but it seems to have made no difference.

updateQueries is still never called (no console.log). I traced this through Chrome’s debugger: it seems that context.hasMissingField is being set to true during diffQueryAgainstStore, causing it to short circuit out of updateQueries. I’m afraid I don’t know enough about that code to be able to help on that front further!

If I use update, I can update the store fine (looking at the next state via the APOLLO_MUTATION_RESULT reducer shows the correct updated store), but it won’t update the props passed down into my component that uses the updated query.

The only reliable option still seems to be refetchQueries.

1reaction
bensalilijamescommented, Apr 10, 2017

@msimulcik Sorry for the delay: I’ve produced a minimal example repo here: https://github.com/benhjames/apollo-fragment-issue

The most important thing about this is that if you remove the ... on ReplyNode and ... on TriggerNode fragments from the query/mutation, it works just fine. As soon as you add in the fragments, the client stops calling updateQueries etc.

Interestingly, somewhere between apollo-client@1.0.0-rc3 and apollo-client@1.0.0, refetchQueries also stopped working.

Do let me know if there’s anything else you need info-wise. Cheers 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using fragments - Apollo GraphQL Docs
Learn how to use fragments to share fields across queries. A GraphQL fragment is a shared piece of query logic.
Read more >
Errors | urql Documentation
This error occurs when no main fragment can be found, because the DocumentNode is maybe empty or does not contain fragments. When you're...
Read more >
useQuery returns undefined, But returns data on gql playground
It seems the problem happens when using fragments (in this case, inline) ... data into the store using update, updateQuery, writeQuery, etc.
Read more >
Using fragments | Vue Apollo
A GraphQL fragment is a shared piece of query logic. ... graphql-anywhere and graphql-tag packages which aim to help us, especially with the...
Read more >
ReadQuery from cache results in Can't find field on object undefined ...
It seems the problem happens when using fragments (in this case, inline) and ... invalid data into the store using update, updateQuery, writeQuery,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found