How to remove the nested input object in the Graphene Django mutation query (Relay)? - graphql

I want to create a Mutation in Relay. I'm using InputObjectType pattern to separate the input and make it reusable.
In mutation class I'm using Input class and there I'm passing the InputObjectType
In general it works but the final query at the client side is very ugly.
I need to pass arguments in this way
query( input : { input : { ...arguments } } )
and to be honest I don't like it. I think it looks ugly.
So the question is: Is it possible to avoid to use a lot of these input objects?
It's ok to use 1 input object, but the nested one is redundant and I'd like to avoid to use it.
Thanks for any help!
Here is the example
class FuelTypeInput(graphene.InputObjectType):
id = graphene.Int()
label = graphene.String()
class FuelSubtypeInput(graphene.InputObjectType):
id = graphene.ID()
label = graphene.String()
fuel_type = graphene.Field(FuelTypeInput)
class CreateFuelSubType(relay.ClientIDMutation):
class Input:
input = FuelSubtypeInput(required=True)
fuel_subtype = Field(FuelSubTypeNode)
ok = graphene.Boolean()
def mutate_and_get_payload(root, info, input):
label = input.label
fuel_type = FuelType.objects.get(pk=input.fuel_type.id)
fuel_subtype = FuelSubType(label=label, fuel_type=fuel_type)
ok = True
return CreateFuelSubType(fuel_subtype=fuel_subtype, ok=ok)
The mutation query is:
mutation MyMutations {
createFuelSubtype( input: { input : { label: "Mutation Subtype", fuelType: {
id: 3
}} } ) {
fuelSubtype {
label
}
ok
}
}
It works fine, here is the result. But I'd like to remove the nested input things
{
"data": {
"createFuelSubtype": {
"fuelSubtype": {
"label": "Mutation Subtype"
},
"ok": true
}
}
}

you can fix with this:
class FuelTypeInput(graphene.AbstractType):
id = graphene.Int()
label = graphene.String()
class CreateFuelSubType(relay.ClientIDMutation):
Input = FuelSubtypeInput
fuel_subtype = Field(FuelSubTypeNode)
ok = graphene.Boolean()
# Other Code ...

Related

Conditional field in GraphQL Where query

Using Apollo client I am trying to run a query that will return students with any status if the status field is empty. If there is a status filter it should be applied:
const statusWhere = inputs.status ? { equals: $inputs.status }: {};
query GetStudents($course: ID, $status: String, $statusWhere: status_bool_exp) {
studentCourses (where :{
status: {$statusWhere},
course: {
id: {
equals: $course
}
},
# other fields, etc
This is giving error:
GraphQLError: Syntax Error: Expected Name, found "$".
Could you provide any hints?
After a bunch of trial and error I was able to figure it out myself. Posting it here because it could be useful for someone else.
Using the regular JS string interpolation variables works here.
So you need to define the condition as a string literal:
const statusWhere = inputs.status ? 'equals: "'+ inputs.status +'"' : '';
Then the whole gpl string looks like this:
gql`
query GetStudents($course: ID) {
studentCourses (where :{
status: {
${statusWhere}
},
course: {
id: {
equals: $course
}
},
})
# fields etc`
In this case you do not need to pass your string variable as a query param.

Alter child collections of two models in Laravel

I have this:
$merge = parents::with('children')->get();
which gives me:
[{
"id":1,
"parent_name":"Robert",
"children":[{
"id":5,"name":"Susan","dob":"2010-11-11"},
{"id":7,"name":"Tony","dob":"2014-12-10"}]
}]
I want to add age into children with expected output like this:
[{
"id":1,
"parent_name":"Robert",
"children":[{
"id":5,"name":"Susan","dob":"2010-11-11","age":"10"},
{"id":7,"name":"Tony","dob":"2014-12-10","age":"7"}]
}]
I tried this:
return $merge->map(function ($detail) {
$detail->age = \Carbon\Carbon::parse($detail->dob)->diffInYears();
return $detail;});
it gives me:
[{
"id":1,
"parent_name":"Robert",
"age":0,
"children":[{
"id":5,"name":"Susan","dob":"2010-11-11"},
{"id":7,"name":"Tony","dob":"2014-12-10"}]
}]
I tried:
return $merge->children->map(function ($detail) {
$detail->age = \Carbon\Carbon::parse($detail->dob)->diffInYears();
return $detail;});
It returns
Property [children] does not exist on this collection instance.
Is there any simple way to achieve my goal?
loop on each parent and its children and add new age property
$data = parents::with('children')->get();
$data->each(function($parent) {
$parent->children->each(function($child){
$child->age = \Carbon\Carbon::parse($child->dob)->diffInYears();
});
});
what I suggest and I think is better is to add an attribute to your Model like so:
class modelName extends Model{
protected $append = ['age'];
public function getAgeAttribute(){
return \Carbon\Carbon::parse($this->dob)->diffInYears();
}
}
Loop through parents and its children. Eventually calculate the age and put the value to array.
$merge->map(function ($mer) {
$mer->children->map(function ($child) {
$age = \Carbon\Carbon::parse($child->dob)->diffInYears();
$child->age = $age;
});
});

Return any data from a query using GraphQL, Graphene and Python

I am receiving the following error:
{
"errors": [
{
"message": "Unknown argument \"project_id\" on field" +
\"get_project_detail_summary\" of type \"Query\".",
"locations": [
{
"line": 2,
"column": 30
}
]
}
]
}
With the following query:
query GetProjectDetailSummary($project_id: Int) {
get_project_detail_summary(project_id: $project_id) {
comments {
... on ManagerCommentNode {
id
text
created
}
... on VendorCommentNode {
id
text
created
}
... on TenantCommentNode {
id
text
created
}
}
}
}
With the following backend code, How can I get to the breakpoint?, or how do I send back custom data given a number?:
class CommentsUnion(graphene.types.union.Union):
class Meta:
name = 'CommentsUnion'
types = (ManagerCommentNode, VendorCommentNode, TenantCommentNode, )
class ProjectSummaryInput(graphene.InputObjectType):
project_id = graphene.Int()
class ProjectSummaryNode(graphene.ObjectType):
Input = ProjectSummaryInput
project_id = graphene.Int()
comments = graphene.List(CommentsUnion)
#classmethod
def resolve_comments(self, *args, **kwargs):
import pdb;pdb.set_trace()
return ProjectSummary.select_related('comments').objects.filter(comments__created__lt=dt)
class Query(graphene.ObjectType):
get_project_detail_summary = Field(ProjectSummaryNode)
In regards to the error.
Be sure to add a kwarg ( e.g. project_id in this example, it is the reason for the "unknown argument on field" error ) to the graphene.Field for get_project_detail_summary.
Like so:
class Query(graphene.ObjectType):
get_project_detail_summary = Field(ProjectSummaryNode, # see below for example
project_id=graphene.Int() # kwarg here
)
def resolve_get_project_detail_summary(self, info, **kwargs):
return ProjectSummary.objects.get(id=kwargs.get('project_id'))
In regards to returning any data.
This is a way ( returning a graphene.String ), but it untypes the response by putting everything inside a String:
from django.core.serializers.json import DjangoJSONEncoder
class ProjectSummaryRecentUpdatesNode(graphene.ObjectType):
Input = ProjectSummaryInput
recent_updates = graphene.String()
def resolve_recent_updates(self, resolve, **kwargs):
instance = Project.objects.get(id=resolve.variable_values.get("project_id"))
things = instance.things.all()
# these are all from different models, and the list is a bit longer than this.
querysets = (
("scheduled", get_scheduled(things, resolve, **kwargs)),
("completed", get_completed(things, resolve, **kwargs)),
("invoices", get_invoices(things, resolve, **kwargs)),
("expenditures", get_expenditures(things, resolve, **kwargs)),
("comments", get_comments(things, resolve, **kwargs)),
("files", get_files(things, resolve, **kwargs)),
)
recent_updates = []
for update_type, qs in querysets:
for item in qs:
item.update(
{
"recent_update_type": update_type
}
)
recent_updates.append(item)
return json.dumps(recent_updates, cls=DjangoJSONEncoder)
And on the frontend, with an import { Query } from "react-apollo"; element we can JSON.parse the field ... which has "any" (json serialized) data inside of it:
<Query
query={GET_PROJECT_DETAIL_SUMMARY_RECENT_UPDATES}
fetchPolicy="network-only"
variables={{ project_id: this.props.projectId }}
>
{({ loading, error, data }: QueryResult) => {
if (
data &&
data.get_project_detail_summary_recent_updates &&
data.get_project_detail_summary_recent_updates.recent_updates
) {
console.log(JSON.parse(data.get_project_detail_summary_recent_updates.recent_updates))
}
}}
</Query>
Side note:
If there isn't a large list of data types create a Union like object which has all of the fields needed from different models, or an actual Union, which seems like the correct way, as then types are not lost:
class ProjectSummaryNode(graphene.ObjectType):
# from FileNode graphene.ObjectType class
file__id = graphene.Int()
file__brief_description = graphene.String()
file_count = graphene.Int()
last_file_created = graphene.String()
last_file = graphene.String()
# from UploaderNode graphene.ObjectType class
uploader__first_name = graphene.String()
uploader__last_name = graphene.String()
# from CommentNode graphene.ObjectType class
comment = graphene.String()
class Meta:
name = "ProjectSummaryNode"
# example of Union, the above fields would be commented out, as they are part of
# the different graphene.ObjectType classes, and this line below would be uncommented
# types = ( FileNode, UploaderNode, CommentNode, )
Still open to suggestions on a better way to do this.

How can i use logical operators with strapi's graphql filtering

I'm passing graphql variables like this:
variables: {
where: {
league: {
name_contains: "some league name"
},
teams: {
name_contains: "some teams name"
}
}
}
I want either the league's name or teams name or both to exist.
but my current configuration is set to have both league's name and team's name to exist
the query looks like this:
const SEARCH_QUERY = gql`
query SEARCH_QUERY($where: JSON) {
games(where: $where) {
id
teams {
id
name
}
league {
id
name
}
}
}
I guess that you are looking some thing like in the howtographql documentation, I can't see on the moment some similar solution in the strapi documentation but I have some another workaround solution...
teams.name && league.name
You need just these arguments
where:{teams: {name_contains: "some league name"}, league: {name_contains: "some league name"}}
teams.name || league.name
In this case you need merge separate queries for each name parent like this:
where:{teams: {name_contains: "some league name"}}
where:{league: {name_contains: "some league name"}}
Body transform function:
const gameMap = [...teamsGames, ...leagueGames].reduce((games, game) => {
games[game.id] = games[game.id] ? games[game.id] : { id: game.id, teams: [], league: [] };
games[game.id].teams = [...games[game.id].teams, ...game.teams];
games[game.id].league = [...games[game.id].league, ...game.league];
return games;
}, {});
//flat to array
Object.keys(gameMap).map(key => gameMap[key]);

Issue with RANGE_ADD in Relay Mutations

I was going through the relay docs and came to following code in RANGE_ADD.
class IntroduceShipMutation extends Relay.Mutation {
// This mutation declares a dependency on the faction
// into which this ship is to be introduced.
static fragments = {
faction: () => Relay.QL`fragment on Faction { id }`,
};
// Introducing a ship will add it to a faction's fleet, so we
// specify the faction's ships connection as part of the fat query.
getFatQuery() {
return Relay.QL`
fragment on IntroduceShipPayload {
faction { ships },
newShipEdge,
}
`;
}
getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'faction',
parentID: this.props.faction.id,
connectionName: 'ships',
edgeName: 'newShipEdge',
rangeBehaviors: {
// When the ships connection is not under the influence
// of any call, append the ship to the end of the connection
'': 'append',
// Prepend the ship, wherever the connection is sorted by age
'orderby(newest)': 'prepend',
},
}];
}
/* ... */
}
Now over here it is mentioned that edgeName is required for adding new node to the connection. Looks well and fine.
Now, I move further down the documentation and reached the GraphQL implementation of this mutation.
mutation AddBWingQuery($input: IntroduceShipInput!) {
introduceShip(input: $input) {
ship {
id
name
}
faction {
name
}
clientMutationId
}
}
Now according to docs this mutation gives me output as
{
"introduceShip": {
"ship": {
"id": "U2hpcDo5",
"name": "B-Wing"
},
"faction": {
"name": "Alliance to Restore the Republic"
},
"clientMutationId": "abcde"
}
}
I cannot see edgeName being present here.
I was using graphene for my project. Over there also I saw something similar only
class IntroduceShip(relay.ClientIDMutation):
class Input:
ship_name = graphene.String(required=True)
faction_id = graphene.String(required=True)
ship = graphene.Field(Ship)
faction = graphene.Field(Faction)
#classmethod
def mutate_and_get_payload(cls, input, context, info):
ship_name = input.get('ship_name')
faction_id = input.get('faction_id')
ship = create_ship(ship_name, faction_id)
faction = get_faction(faction_id)
return IntroduceShip(ship=ship, faction=faction)
Over here also I cannot see edgeName anywhere.
Any help please? I am working on mutations for the first so wanted to confirm a m I missing something or is something wrong here?
This example might be either simplified or a bit obsoloete, because in practice there is need to return edge and that's exactly what is fetched by relay (other fields in RANGE_ADD are more a kind of declaration and are not necessarily fetched).
Here is how you can do it in graphene:
# Import valid as of graphene==0.10.2 and graphql-relay=0.4.4
from graphql_relay.connection.arrayconnection import offset_to_cursor
class IntroduceShip(relay.ClientIDMutation):
class Input:
ship_name = graphene.String(required=True)
faction_id = graphene.String(required=True)
ship = graphene.Field(Ship)
faction = graphene.Field(Faction)
new_ship_edge = graphene.Field(Ship.get_edge_type().for_node(Ship))
#classmethod
def mutate_and_get_payload(cls, input, context, info):
ship_name = input.get('ship_name')
faction_id = input.get('faction_id')
ship = create_ship(ship_name, faction_id)
faction = get_faction(faction_id)
ship_edge_type = Ship.get_edge_type().for_node(Ship)
new_ship_edge = edge_type(
# Assuming get_ships_number supplied
cursor=offset_to_cursor(get_ships_number())
node=ship
)
return cls(ship=ship, faction=faction, new_ship_edge=new_ship_edge)

Resources