apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

GraphQL documents with multiple queries + operationName throws exception #66

Open Fi1osof opened 5 years ago

Fi1osof commented 5 years ago

Migrated from: apollographql/apollo-client#3556

I use https://github.com/graphql/graphiql and create fetcher with apollo-client.

fetcher = async (props) => {

  const {
    query,
    ...other
  } = props;

  const {
    client,
  } = this.context;

  return await client.initQueryManager().query({
    query: gql(query),
    ...other
  })
  .catch(error => {
    console.error(error);
    throw error;
  })
}

Single query executed fine.

query users{users(where:{id: "dsf"}){
  id
}}

But multiple throws exception.

query users{users(where:{id: "dsf"}){
  id
}}

mutation createUser{
  createUser(
    data:{ }
  ){
    id
  }
}

Client props:

{"query":"\n\nquery users{users(where:{id: \"dsf\"}){\n  id\n}}\n\nmutation createUser{\n  createUser(\n    data:{ }\n  ){\n    id\n  }\n}","variables":null,"operationName":"users"}

Exception:

Error: Ambiguous GraphQL document: contains 2 operations
    at checkDocument (getFromAST.js:31)
    at getOperationDefinition (getFromAST.js:35)
    at getQueryDefinition (getFromAST.js:62)
    at QueryManager.watchQuery (QueryManager.js:639)
    at QueryManager.js:682
    at new Promise (<anonymous>)
    at QueryManager.query (QueryManager.js:679)
    at _callee$ (App.js:89)
    at tryCatch (runtime.js:62)
    at Generator.invoke [as _invoke] (runtime.js:288)
    at Generator.prototype.(:3000/anonymous function) [as next] (http://localhost:3000/static/js/1.chunk.js:697:21)
    at asyncGeneratorStep (asyncToGenerator.js:3)
    at _next (asyncToGenerator.js:25)
    at asyncToGenerator.js:32
    at new Promise (<anonymous>)
    at asyncToGenerator.js:21
    at App.js:50
    at QueryBuilder._fetchQuery (GraphiQL.js:628)
    at Object.handleRunQuery [as onRun] (GraphiQL.js:792)
    at ExecuteButton._this._onOptionSelected (ExecuteButton.js:105)
    at onMouseUp (ExecuteButton.js:173)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:265)
    at executeDispatch (react-dom.development.js:622)
    at executeDispatchesInOrder (react-dom.development.js:647)
    at executeDispatchesAndRelease (react-dom.development.js:747)
    at executeDispatchesAndReleaseTopLevel (react-dom.development.js:760)
    at forEachAccumulated (react-dom.development.js:727)
    at runEventsInBatch (react-dom.development.js:903)
    at runExtractedEventsInBatch (react-dom.development.js:913)
    at handleTopLevel (react-dom.development.js:5078)
    at batchedUpdates$1 (react-dom.development.js:19041)
    at batchedUpdates (react-dom.development.js:2307)
    at dispatchEvent (react-dom.development.js:5158)
    at interactiveUpdates$1 (react-dom.development.js:19103)
    at interactiveUpdates (react-dom.development.js:2328)
    at dispatchInteractiveEvent (react-dom.development.js:5134)
barruda commented 5 years ago

is there a way to have multiple queries on a single file? having a file per query is really annoying

dylanwulf commented 5 years ago

is there a way to have multiple queries on a single file? having a file per query is really annoying

@barruda Yes, if you use the gql tag from the graphql-tag package then you can keep multiple queries/fragments/mutations in the same file.

barruda commented 5 years ago

Thanks for the fast answer, I was forgetting the {} around the import.. so there was no way to know which query I was pointing to... for reference the right use should be like: import { GetConteudoByTagNome } from '~/apollo/queries/tag' where GetConteudoByTagNome is the name of the query