domasx2 / graphql-combine-query Goto Github PK
View Code? Open in Web Editor NEWcombine multiple graphql queries or mutations into a single one
combine multiple graphql queries or mutations into a single one
For browser usage what would be the equivalent of print() for retrieving the combined literal for posting to the graphQL server?
I try to use it with default example and nodejs but I got this strange error ...
>node graphql-combine-query_test_001.mjs
file:///E:/testgithubgraphql/graphql-combine-query_test_001.mjs:20
const { document, variables } = combineQuery('FooBarQuery')
^
TypeError: combineQuery is not a function
at file:///E:/testgithubgraphql/graphql-combine-query_test_001.mjs:20:33
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:526:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
I'm having some trouble combining queries that have the same variable
. https://github.com/domasx2/graphql-combine-query/blob/master/src/index.ts#L68
Looks like this is already supported by addN
, is there any reason why does not work with add
?
Happy to contribute
This is the error i am getting
node_modules/graphql-combine-query/dist/index.d.ts:6:136 - error TS2344: Type 'TVariables' does not satisfy the constraint 'OperationVariables'.
6 add: <TData = any, TVariables = OperationVariables>(document: DocumentNode, variables?: TVariables) => CombinedQueryBuilder<TData, TVariables>;
~~~~~~~~~~
node_modules/graphql-combine-query/dist/index.d.ts:6:24
6 add: <TData = any, TVariables = OperationVariables>(document: DocumentNode, variables?: TVariables) => CombinedQueryBuilder<TData, TVariables>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This type parameter might need an `extends OperationVariables` constraint.
Found 1 error in node_modules/graphql-combine-query/dist/index.d.ts:6
Here is my tsconfig
{
"compilerOptions": {
"rootDirs": ["src"],
"outDir": "dist",
"lib": ["es2020"],
"target": "es2020",
"module": "esnext",
"moduleResolution": "node",
"types": ["node", "jest"],
"resolveJsonModule": true,
"esModuleInterop": true,
"strictNullChecks": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"dist",
"jest.config.ts",
"src/tests/*",
"jest-setup.ts"
]
}
I am using node18
Hi There,
I followed this Stack Overflow link here as I want to do exactly the same as the author of that question:
but when I try to implement his example it fails for me with a duplicate field definition post for oprations Query1 and Query2
error:
tap.test('queries can be combined', (test) => {
// given
const query1 = gql`
query Query1 {
post(id: 1234) {
title
description
}
}
`
const query2 = gql`
query Query2 {
post(id: 1234) {
tags
author {
name
}
}
}
`
const { document } = combineQuery('CombinedQuery').add(query1).add(query2)
// then
test.equals(
print(document),
print(gql`
query CombinedQuery {
post(id: 1234) {
title
description
tags
author {
name
}
}
}
`)
)
test.end()
})
I realise that you have some fancy logic for merging query variables which may have something to do with why you have this top-level restriction, or is this something that can be relaxed so you can also support merging of overlapping queries (without having to use fragments of course)?
This could be supported adding an alias for duplicate fields
It says "grapqhl" instead of "graphql"
Can we have a dist version (js) ?
Thanks in advance !
I am generating forms based on a config (object) and some of the inputs would have queries in them to display foreign data in the dropdown menu inputs
so I have a loop that finds the inputs that need foreign data and I merge them into one final query, however there will be
an arbitrary initial query schema that I need to use to make it work, since I can't merge it with an initial empty schema.
in this case the companiesQuery is the initial schema
let companiesQuery = gql`query { companies { id: company_id, company_name } }`
let finalSchema = companiesQuery // need arbitrary schema so first merge will work
config.formFields.forEach(each => {
if (each.foreignQuery) {
const { document } = combineQuery('CombinedQuery').add(finalSchema).add(each.foreignQuery)
finalSchema = document
}
})
this works perfectly fine but it would be nice not to have an extra call somehow because its causing me other problems other than using extra resources.
I understand if this isn't the scope of the library. But there is one more thing which I wanted to mention, with the code above, it does work perfectly fine but I am getting warnings which I would like to get rid of
WARNING in ./node_modules/graphql-combine-query/dist/index.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'C:\Users\<retracted>\<retracted>\<retracted>\frontend\node_modules\graphql-combine-query\src\index.ts' file: Error: ENOENT: no such file or directory, open 'C:\Users\<retracted>\<retracted>\<retracted>\frontend\node_modules\graphql-combine-query\src\index.ts'
let me know if you need extra info, and thanks a ton for this library!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.