Mock GraphQLUpload dataype in POSTMAN - graphql

I am using Postman to send GraphQL queries to my graphQL server. This is what a particular mutation schema looks like:
extend type Mutation {
createMutation(
my_id: ID!
my_data: input_data
): some_output
input input_data {
some_key: int
file : Upload!
}
I am able to perform other mutations and query through graphQL by defining appropriate GraphQL variables in here
I am not sure how to create a json value in GraphQL Variables for "file" of type Upload

Related

Querying all objects in Amplify mock api and getting null

I am using Amplify in a simple use case to mock an existing frontend. I have a cutdown schema.graphql as follows:
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } }
schema {
query: Query
}
type Query {
getAirports: [Airport]
}
type Airport #model {
id: Int! #primaryKey
code: String!
city: String!
country: String!
}
The getAirports query is intended to return all the airports. I run amplify mock api and it generates all the resolvers.
When I navigate to http://localhost:20002, I can see the option to use getAirports, however it returns null even when data is present in the mocked database. The response is
{"data":null,"errors":[{"message":"Cannot return null for non-nullable field Query.getAirports.","locations":[{"line":2,"column":3}],"path":["getAirports"]}]}
I'm curious how I can write the schema to have a getAirports query in a way that it returns data a full list of Airports similar to listAirports which is created by default.

GraphQL | How to implement conditional nesting?

Please consider the following GraphQL schema:
type User {
id: ID!
events: [Event]
}
type Event {
id: ID!
user: User!
asset: Asset!
}
type Asset {
id: ID
price: Number!
name: String!
}
GraphQL is a fantastic framework for fetching nested objects, but I'm struggling to understand how conditional nesting is implemented.
Example:
I want to retrieve all events for a specific user where asset.price is greater than x.
Or
I want to retrieve all events for an asset that belongs to a list of users [].
Question: Is conditional nesting a concept in GraphQL and how is it implemented?
Side note: I use AWS AppSync and resolvers are fetching data from AWS DynamoDB.
You can define a filter/condition on any GraphQL query such as:
query {
users(permission: 'ADMIN') {
...
}
}
The permission param is passed to your resolver (say DynamoDb VTL template, Lambda etc) to be handled however you want - to GQL this is just another parameter.
You can carry this concept into nested field by creating an events resolver and you'd then call it like this:
query {
user(id: '123') {
name
events(minPrice: 200) {
nodes: {
id
eventName
eventDate
}
}
dob
...
}
}
In above case I am using a simple minPrice param but you could do more complex things such price ranges, even pass operators (eq, gt, ...). It's all irrelevant to GraphQL - all gets passed to the resolver.
How you implement that on backend depends on your setup. I use AppSync without Amplify and write my own VTL templates and build the DynamoDb request using the provided GQL fields.
Here is an SO post that shows how to create a date filter.

AWS CDK + Appsync nested lambda resolvers arguments

I'm building a graphql API on AWS using CDK, resolvers are custom lambdas. One type in my schema.graphql has got a field with its own inputs. The schema looks something like this.
type Output {
field1: Int!
field2: String
field3(input1: String!, input2: String!): Int
}
input MyInput {...}
type Mutation {
MyMutation(input: MyInput): Output
}
Currently I'm only declaring the main MyMutation resolver in the CDK script.
The arguments for field3 are inside event.info.selectionSetGraphQL, but are formatted as strings, like field3(input1: 'str1', input2: 'str2)\n.
Shouldn't I be able to get them in some sort of arguments field as an object as it is for the arguments of the top level MyMutationfield?
Is this because I'm not using field resolvers properly?
I could not find examples of nested graphql resolvers online. Is there a better way of doing this? Something like Apollo's resolver maps?
Thank you.

can I assign a variable in a graphql playground to the result of a mutation

If I have a mutation with 2 fields:
type Mutation {
createSimulation(
name: String
simulators: [AvailableSimulators!]!
timeToLiveInMS: Int
): Simulation!
create(
simulationID: ID!
simulator: AvailableSimulators!
type: String!
attributes: KeyValuePair
): CreateResult!
}
When I run the mutation in the graphql applo server playground, I need a value from the return of createSimulation in a call to create:
Can I somehow assign a variable that I can use in create?
Not part of the GraphQL standard. Should handle this in the resolver in the backend. It will depend on the technology used, but in most of the technologies you can use results from previous resolvers or call resolvers manually.
No, you should send 2 requests to handle it. The client-side should request createSimulation first to get a response then request another request to create mutation with that UUID.

Apollo graphQL - can you query local state using variables without having to use a resolver?

I am using apollo-cache-inmemory, apollo-client, react-apollo.
My local state contains a users array like so: -
users: [{
__typename: "User",
userId: "hashid1",
...
},
{
__typename: "User",
userId: "hashid2",
...
}]
Now I can obviously run a simple query to retrieve all the users from the local state: -
import gql from "graphql-tag"
export default gql`{users #client {userId}}`
However, what I would like to do is to be able to query the users array directly, passing variables like so: -
const userDetails = await client.query({ query: USER_DETAILS, variables: {id: "hashId1"}})
Is it possible to run this query without using a resolver? I have attempted the following but { data } returns as null: -
export default gql`query user($id: String!) {users(userId: $id) #client {userId}}`
I already use resolvers and can easily write one to take care of this issue but I am wondering if there it is possible to perform this task without one?
It looks like you're looking for some magic ;)
You must write customization code (overwrite default resolver - return all records) to have a customized behavior (return data filtered by your criteria). That should be obviuos.
There is no default/ready/built in searching/filtering syntax in graphql - therefore, there is no default behaviours for them in apollo-client (no matter local/remote server/data). It is up to you to implement what you need.

Resources