The significance of the string immediately after query type (query / mutation) GraphQL - graphql

I was wondering what the significance of the string that follows the query type, in this case "ProvisionQueues", it seems removing this from the string doesn't affect anything - is it just for logging or something. meta data?
mutation ProvisionQueues {
createQueue(name: "new-queue") {
url
}
}

That string is the operation name. If you don't specify a name, the operation is known as an anonymous operation. When practical I like to always specify an operation name though, because it makes it easier for doing things like reading stack traces.
it seems removing this from the string doesn't affect anything
You can only use an anonymous operation when you are performing a single operation. For instance, the following results in an error:
query {
user(id: 1) {
name
}
}
query {
user(id: 2) {
name
}
}
The error:
"message": "This anonymous operation must be the only defined operation."
If you want to learn more, you can check out the GraphQL spec:
If a document contains only one operation, that operation may be unnamed or represented in the shorthand form, which omits both the query keyword and operation name. Otherwise, if a GraphQL query document contains multiple operations, each operation must be named.

Adding to #Eric's answer with another example.
query allNotifications {
notifications {
success
errors
notifications {
id
title
description
attachment
createdAt
}
}
} ​
​
query {
users {
errors
success
users {
id
fullName
}
}
}
Notice above that the users query has no operation name. This can be resolved as below.
​
query allUsers {
users {
errors
success
users {
id
fullName
mohalla
}
}
}

Related

How can I modify a graphQL query based off of the variable passed to it?

I have built this query to request data from the Pokeapi graphQL server here
https://beta.pokeapi.co/graphql/console/
query getPokemon (
$typesList: [Int!],
) {
pokemon_v2_pokemon(
where: {
pokemon_v2_pokemontypes: { type_id: { _in: $typesList } }
}
) {
id
name
pokemon_v2_pokemontypes {
type_id
pokemon_v2_type {
name
}
}
}
}
How can I modify the query to return all results when the $typesList list is empty? I would like the query to return a filtered results list when $typesList has a list of ids, but it should return all values when the $typesList list is empty.
Is this something that is possible in graphQL?
Thank you
If you controlled the server you could write the resolver to function in the manner you're looking for but since you don't your only option is to wrap your use of the query with something that sets the default typesList variable to all possible types.

GraphQL: Remove field from query string

I have a graphQL server consumed by many clients and have a process that generates operations from introspection; problem is that the schema has a handful of nested queries that take arguments that I may or may not want to invoke.
given this generated operation
query Query($input: String!) {
things(input: $input) {
item1
item2
nestedQuery(nestedInput: $nestedInput) {
nestedItem1
}
}
}
is there a tool (other than string parsing, that's a given) to strip unwanted fields from here so that the query is transformed into
query Query($input: String!) {
things(input: $input) {
item1
item2
}
}
Looking at the introspection JSON I can identify this item by the presence of args on the field things that is past the first depth - but I don't want to necessarily lose information on generation - I'd like those nested queries to be visible/documented.
I still don't understand how you are generating the GraphQL document here, but assuming that you have total control over it, you can use the default directives (#skip and #include) to skip or include fields based on a condition.
For example, the following document will skip the nestedQuery field when executing. You may use a variable value as the value for the argument if.
query Query($input: String!) {
things(input: $input) {
item1
item2
nestedQuery(nestedInput: $nestedInput) #skip(if: true) {
nestedItem1
}
}
}

What is `... on` doing in this GraphQL?

I am trying to mimic what some GraphQL does, but I do not have access to be able to run the original. It is of the form:
query {
dataSources(dataType: Ais) {
... on AisDataSource {
messages(filter: {broadcastType: Static}) {
... on AisStaticBroadcast {
field1
field2
(I have ommitted the closing parentheses).
It is my understanding that ... on is either to include a fragment (none here), or to choose between alternatives (but these are nested). So is this query wrong, or is there more to ... on?
This
{
user {
... on User {
id
username
}
}
}
and this
{
user {
...UserFragment
}
}
fragment UserFragment on User {
id
username
}
are equivalent. In both cases, you are using a fragment. In the first example, we simply refer to the fragment as an inline fragment.
When requesting a field that return a composite type (an object, interface or union), you must specify a selection set, or one or more fields for the return type. Since fragments must include a type condition (the on keyword plus the type name), they can be used to specify different selection sets depending on the type that's actually returned at runtime.
{
user {
...RegularUserFragment
...AdminFragment
}
}
fragment RegularFragment on RegularUser {
id
username
}
fragment AdminFragment on Admin {
id
username
accessLevel
}
All we're saying is "if the type at runtime is this, then return this set of fields". If any of the fields inside the fragment also return a composite type, then those fields also have to specify a selection set for -- that means additional fragments can be used inside those selection sets.

How can I execute an instance query in graphql-fhir?

This issue is migrated from a question on our Github account because we want the answer to be available to others. Here is the original question:
Hello,
Following is the InstanceQuery I tried
http://localhost:3000/3_0_1/Questionnaire/jamana/$graphql?query={id}
I am receiving back response as Cannot query field \"id\" on type \"Questionnaire_Query\"
So what is the right format I should try ?
https://build.fhir.org/graphql.html has a sample as http://test.fhir.org/r3/Patient/example/$graphql?query={name{text,given,family}}.Its working in their server. I cannot get the response When I try similarly in our graphql-fhir.
Original answer from Github:
We are using named queries since we are using express-graphql. I do not believe that is valid syntax. Also, the url provided does not seem to work, I just get an OperationOutcome saying the patient does not exist, which is not a valid GraphQL response.
Can you try changing your query from:
http://localhost:3000/3_0_1/Questionnaire/jamana/$graphql?query={id}
to this:
http://localhost:3000/3_0_1/Questionnaire/jamana/$graphql?query={Questionnaire{id}}
When writing the query, you need to provide the return type as part of the instance query. You should get a response that looks like similar to this(if you have implemented your resolver you will have data and not null):
{
"data": {
"Questionnaire": {
"id": null
}
}
}
and from a later comment:
If you are getting null then you are doing it correctly, but you haven't wrote a query or connected it to a data source. You still need to return the questionnaire in the resolver.
Where you are seeing this:
instance: {
name: 'Questionnaire',
path: '/3_0_1/Questionnaire/:id',
query: QuestionnaireInstanceQuery,
},
You are seeing the endpoint being registered with an id parameter, which is different from a GraphQL argument. This is just an express argument. If you navigate to the questionnaire/query.js file, you can see that the QuestionnaireInstanceQuery query has a different resolver than the standard QuestionnaireQuery. So in your questionnaire/resolver.js file, if you want both query and instance query to work, you need to implement both resolvers.
e.g.
// This is for the standard query
module.exports.getQuestionnaire = function getQuestionnaire(
root,
args,
context = {},
info,
) {
let { server, version, req, res } = context;
// Do query and return questionnaire
return {};
};
// This one is for a questionnaire instance
module.exports.getQuestionnaireInstance = function getQuestionnaireInstance(
root,
args,
context = {},
info,
) {
let { server, version, req, res } = context;
// req.params.id is your questionnaire id, use that for your query here
// queryQuestionnaireById does not exist, it is pseudo code
// you need to query your database here with the id
let questionnaire = queryQuestionnaireById(req.params.id);
// return the correct questionnaire here, default returns {},
// which is why you see null, because no data is returned
return questionnaire;
};

How can I do a WpGraphQL query with a where clause?

This works fine
query QryTopics {
topics {
nodes {
name
topicId
count
}
}
}
But I want a filtered result. I'm new to graphql but I see a param on this collection called 'where', after 'first', 'last', 'after' etc... How can I use that? Its type is 'RootTopicsTermArgs' which is likely something autogenerated from my schema. It has fields, one of which is 'childless' of Boolean. What I'm trying to do, is return only topics (a custom taxonomy in Wordpress) which have posts tagged with them. Basically it prevents me from doing this on the client.
data.data.topics.nodes.filter(n => n.count !== null)
Can anyone direct me to a good example of using where args with a collection? I have tried every permutation of syntax I could think of. Inlcuding
topics(where:childless:true)
topics(where: childless: 'true')
topics(where: new RootTopicsTermArgs())
etc...
Obviously those are all wrong.
If a custom taxonomy, such as Topics, is registered to "show_in_graphql" and is part of your Schema you can query using arguments like so:
query Topics {
topics(where: {childless: true}) {
edges {
node {
id
name
}
}
}
}
Additionally, you could use a static query combined with variables, like so:
query Topics($where:RootTopicsTermArgs!) {
topics(where:$where) {
edges {
node {
id
name
}
}
}
}
$variables = {
"where": {
"childless": true
}
};
One thing I would recommend is using a GraphiQL IDE, such as https://github.com/skevy/graphiql-app, which will help with validating your queries by providing hints as you type, and visual indicators of invalid queries.
You can see an example of using arguments to query terms here: https://playground.wpgraphql.com/#/connections-and-arguments

Resources