generated from sysgears/apollo-universal-starter-kit
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Query
r-kohale9 edited this page Dec 18, 2019
·
2 revisions
Define the Query in the schema.graphql with properly defined input (if any and which can be an Object Entity or single attribute like string, number, etc), and the Entity ( object, number, string, boolean, etc) that needs to be returned.
exampleQuery(input: ExampleQueryInput): ReturnEntity
-
exampleQuery
is the name of the query (which can be anything of your choice, but well, try to keep it related to the context to avoid confusion (We do not want a horse query requesting for a monkey. Lol.). -
ExampleQueryInput
is the input type which can be anything from string to boolean or number or even user-defined object. - Suppose it is an user defined object, we need to define it in the same file as follows:
input ExampleQueryInput {
inputA: inputAtype!
inputB: inputBtype
inputC: [inputCtype]
}
- The
!
means that we cannot skip this input, else not necessary for proper execution. - The input-types can again be string, boolean, number, user-defined object, etc. or even array which is shown by
[ ]
. -
ReturnEntity
can be anything like string, boolean, number, user-defined object, array, etc. Suppose it is user-defined, it first needs to defined as follows:
type ReturnEntity {
inputA: inputAtype!
inputB: inputBtype
inputC: [inputCtype]
}
Notes:
- Keep in mind to use type and input keywords appropriately.
- It is preferable to use user-defined entity for input even if it has one attribute. It'll help extend the no of input attributes in future if necessary, without changing the query.
Now the functioning of the query needs to defined in the resolver.ts(/.js) under the Query keyword/object with the same name as the one defined in the schema.graphql.
async exampleQuery(obj: any, { input }: any, context: any)
{
// some logic checks and function calls defined in sql.ts(/.ts) of that module for database actions
return ReturnEntity
}
- Here
async
is optional and is used for proper flow of actions. -
exampleQuery
is the name of the query - input is of type
ExampleQueryInput
Now define the functions required for the database action for that mutation in the sql.ts(/.ts) file in that module within proper class.
Now the query can be used in the client side