So I have a table of users where each user is associated with a main tool and tools in their backpacks. Each tool also have a "quality". But I have no idea of how to fetch this value so I appreciate any help on how to do this.
Databases
users
+----+------+--------------+
| id | name | main_tool_id |
+----+------+--------------+
| 1 | adam | 1 |
+----+------+--------------+
tools
+----+-------------+
| id | name |
+----+-------------+
| 1 | hammer |
| 2 | screwdriver |
+----+-------------+
user_tools
+----+---------+---------+---------+
| id | user_id | tool_id | quality |
+----+---------+---------+---------+
| 1 | 1 | 1 | LOW |
| 2 | 1 | 2 | HIGH |
+----+---------+---------+---------+
Models
type User struct {
Id int64 `json:"-"`
Name string `json:"name"`
MainToolId int64 `json:"-"`
MainTool Tool `json:"main_tool"`
ToolsInBackpack []Tool `json:"tools_in_backpack" gorm:"many2many:user_tools"`
}
type Tool struct {
Id int64 `json:"-"`
Name string `json:"name"`
Quality string `json:"quality"`
}
Code
var users []User
DB.Preload("MainTool").Preload("ToolsInBackpack").Find(&users) // <-- Modify this (I guess)
Real result
{
"name": "adam",
"main_tool": {
"name": "hammer",
"quality": ""
},
"tools_in_backpack": [
{
"name": "hammer",
"quality": ""
},
{
"name": "screwdriver",
"quality": ""
},
]
}
Desired result
{
"name": "adam",
"main_tool": {
"name": "hammer",
"quality": "LOW"
},
"tools_in_backpack": [
{
"name": "hammer",
"quality": "LOW"
},
{
"name": "screwdriver",
"quality": "HIGH"
},
]
}
Thanks a lot for your help!
If you do not require MainTool to return an object.
maybe you uses it
type User struct {
ID uint `json:"-"`
Name string
MainTool int64
ToolsInBackpack string
Tools []*Tool
}
type Tool struct {
ID uint`json:"-"`
Name string
UserID uint
}
User{MainTool: 1, ToolsInBackpack: "[1,2]", Tools: []*Tool{&t1, &t2}}
if you don't wana to use string at ToolsInBackpack string you can't use datatypes.JSON at gorm.io/datatypes
for example
ToolsInBackpack datatypes.JSON
This isn't supported in Gorm so there won't be an easy way to just Preload and it's done.
Here are a few techniques you could look at:
Use a Custom Join Table Model that contains the fields that you need
type UserTool struct {
UserID int64 `gorm:"primaryKey"`
ToolID int64 `gorm:"primaryKey"`
Quality string
}
err := db.SetupJoinTable(&User{}, "ToolsInBackpack", &UserTool{})
// handle error
(I left out the UserTools.ID field, Gorm doesn't need it, and you only need it if a user can have the same tool twice, in which case put it back on as part of the primaryKey. I don't know whether Gorm will be happy with it but try it out).
Then you can use this model to query the field as any other model:
userTools := []UserTool
err := db.Where("user_id = ?", user.ID).Find(&userTools).Error
// handle error
// now userTools[i].Quality is filled, you could use it to update the corresponding users[j].ToolsInBackpack[k].Quality
It's a pain because you need to match IDs as a post-processing step.
Use a Has-Many/Belongs-To hybrid relation to model the Many-To-Many Join Table:
Here the User Has Many UserTools and a Tool belongs to one or more UserTools. This effectively models the meaning of Many-to-Many (in ERD, a relationship like [User]>--<[Tool] can be decomposed into [User]--<[UserTool]>--[Tool]).
type User struct {
ID int64
Name string
MainToolId int64
MainTool Tool
ToolsInBackpack []UserTool
}
type UserTool struct {
UserID int64 `gorm:"primaryKey"`
ToolID int64 `gorm:"primaryKey"`
Tool Tool
Quality string
}
type Tool struct {
ID int64
Name string
}
Now you can Preload this association like this:
err := db.Model(&User{}).Preload("ToolsInBackpack.Tool").Find(&users).Error
// handle error
// now users[i].ToolsInBackpack[j].Quality and
// users[i].ToolsInBackpack[j].Tool.Name will be set correctly.
Only problem now is that you've got a weird shape in the model that you're then trying to marshal into JSON (most likely to be used in an API). My advice here is to split the DB model from the JSON API model, and have a mapping layer in your code. The DB model and the API Messages invariably diverge in important ways and trying to reuse one model for both soon leads to pain.
Related
I'm having a GraphQL issue that popped up recently and I'm not sure where to go since I believe i'm following the GraphQL documentation correctly on querying.
I have the following model.
model Contract {
id Int #id #default(autoincrement())
userId Int?
vin String
make String
model String
year Int
ownersName String #default("")
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
ownersEmail String #default("")
User User? #relation(fields: [userId], references: [id])
Dealership Dealership #relation(fields: [dealershipId], references: [id])
notes Note[]
dealershipId Int
}
Here are the related types
type Contract {
id: Int!
vin: String!
make: String!
model: String!
year: Int!
ownersName: String!
User: User
userId: Int
createdAt: DateTime
updatedAt: DateTime
ownersEmail: String
}
type Query {
contractsByUser: [Contract]
}
and here is my query which i'm exporting from a cell
export const QUERY = gql`
query ContractByUser {
contractsByUser {
id
vin
make
model
year
ownersName
createdAt
}
}
I get the following error.
api | ERROR [2021-08-04 13:28:03.477 +0000] (apollo-graphql-server): GraphQL didEncounterErrors
api | errors: [
api | {
api | "message": "Cannot query field \"contractsByUser\" on type \"Query\".",
api | "locations": [
api | {
api | "line": 2,
api | "column": 3
api | }
api | ]
api | }
api | ]
api | INFO [2021-08-04 13:28:03.479 +0000] (apollo-graphql-server): GraphQL willSendResponse
api |
api | Error: Cannot query field "contractsByUser" on type "Query".
api |
api |
api | POST /graphql 400 21.533 ms - 1329
api |
api | GraphQLError: Cannot query field "contractsByUser" on type "Query".
This is definitely a validation error, but I'm not sure what i've missed or done incorrectly. I'm on the latest Version of Redwood and updated the breaking changes. Any help would be good. Maybe its not generating types correctly or.... any help would be appreciated.
I had redwood graphql function misconfigured and it wasn't importing the generated types.
Where I was:
import schemas from 'src/graphql/**/*.js'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import services from 'src/services/**/*.js'
Where I needed to be:
import schemas from 'src/graphql/**/*.{js,ts}'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import services from 'src/services/**/*.{js,ts}'
I'm using Gorm and Golang to fetch data from my database. Is it possible to make Gorm fetch also the objects children (foreign keys)?
Database tables
users
+----+---------+------------+
| id | name | country_id |
+----+---------+------------+
| 1 | Adam | 1 |
| 2 | Bertil | 1 |
| 3 | Charlie | 2 |
+----+---------+------------+
countries
+----+--------+
| id | name |
+----+--------+
| 1 | Sweden |
| 2 | Norway |
+----+--------+
Models
type User struct {
Id int64 `json:"-"`
Name string `json:"name"`
CountryId int64 `json:"-"`
Country Country `json:"country"`
}
type Country struct {
Id int64 `json:"-"`
Name string `json:"name"`
}
Code to fetch all users
var users []User
DB.Find(&users) // Question: How should this be modified to automatically fetch the Country?
Actual result
[
{
"name": "Adam",
"country" : {
"name": "",
}
},
...
]
Desired result
[
{
"name": "Adam",
"country" : {
"name": "Sweden",
}
},
...
]
Thanks a lot for you input!
/Klarre
Yes it is possible, it's called Preloading.
users := make([]User,0)
DB.Preload("Country").Find(&users)
I need help figuring out the GraphQL tag for use with Apollo Client. The Docs don't go far beyond the most basic use case for mutations.
My goal is to have the only required input be an email. If the other variables are present, I would like those to be accepted and create a proposal with all that information.
I have the mutation (in both only email and full variables scenarios) working successfully on the GraphQl Playground (if it helps, you can find it here and test it out, look at the schema, etc.,): https://prisma2-graphql-yoga-shield.now.sh/playground)
mutation {
createOneProposal(
data: {
email: "fake#gmail.com"
name: "Sean"
types: {
create: {
model: PURCHASE
name: "e-commerce"
cost: 600
services: {
create: [
{ service: "Responsive web design" }
{ service: "Another service!" }
{ service: "And yet another service!" }
]
}
}
}
}
) {
created_at
proposal_id
types {
cost
model
name
type_id
services {
service
service_id
}
}
}
}
Producing as a result:
{
"data": {
"createOneProposal": {
"created_at": "2020-02-27T21:28:53.256Z",
"proposal_id": 35,
"types": [
{
"cost": 600,
"model": "PURCHASE",
"name": "e-commerce",
"type_id": 6,
"services": [
{
"service": "Responsive web design",
"service_id": 10
},
{
"service": "Another service!",
"service_id": 11
},
{
"service": "And yet another service!",
"service_id": 12
}
]
}
]
}
}
}
My initial design for the gql tag:
export const NEW_PROPOSAL = gql`
mutation createOneProposal(
$email: String!
$name: String
$cost: Int
$model: Model
$service: Service
) {
createOneProposal(
email: $email
name: $name
cost: $cost
model: $model
service: $service
) {
created_at
proposal_id
types {
services {
service_id
}
}
}
}
`;
But, I get a lot of errors with this.
{"errors":[
{"Variable "$service" cannot be non-input type `"Service`".","locations":[{"line":1,"column":97}]},
{"Unknown argument "email" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":21}]},
{"Unknown argument "name" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":36}]},
{"Unknown argument"cost" on field "createOneProposal\" of type "Mutation`".","locations":[{"line":2,"column":49}]},
{"Unknown argument "model" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":62}]},
{"Unknown argument "service" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":77}]},
{"Field "createOneProposal" argument "data" of type "ProposalCreateInput!`" is required, but it was not provided.","locations":[{"line":2,"column":3}]}]}
So, how can I go about this... I figured out the query version (much easier...), but I just can't figure this out!
My schema, if it helps:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("MYSQL_URL_PRISMA2")
}
model Post {
content String #default("")
created_at DateTime #default(now())
post_id Int #default(autoincrement()) #id
published Boolean #default(false)
published_at DateTime?
title String #default("")
author User
}
model Profile {
bio String?
profile_id Int #default(autoincrement()) #id
user_id User
}
model Proposal {
email String #unique
name String?
proposal_id Int #default(autoincrement()) #id
created_at DateTime #default(now())
types Type[]
}
model Type {
cost Int?
name String?
model Model? #default(SUBSCRIPTION)
services Service[]
type_id Int #default(autoincrement()) #id
proposal_id Proposal
}
model Service {
service_id Int #default(autoincrement()) #id
service String?
type_id Type
}
model User {
email String #default("") #unique
name String #default("")
password String #default("")
role Role #default(USER)
user_id Int #default(autoincrement()) #id
posts Post[]
profiles Profile[]
}
enum Role {
USER ADMIN
}
enum Model {
SUBSCRIPTION PURCHASE CUSTOM
}
GraphQL types are categorized as either input types or output types. Input types are used for inputs like variable definitions or argument definitions. Output types are used for typing fields, which are what compose the actual response. Certain types, like scalars and enums, can be used as either an input or an output. However, with objects, there are output object types (sometimes referred to just object types or objects) and input object types.
Service is an output type, so it can't be used where an input type is expected (in this case, a variable definition). Examine the schema generated by Prisma to determine the appropriate type to use.
Thanks to some very needed direction from #xadm, I figured out the structure of the tag! For anyone who comes across this in the future:
mutation createOneProposal($input: ProposalCreateInput!){
createOneProposal(data:$input){
created_at
name
email
proposal_id
type{
cost
description
model
name
type_id
services{
service
cost
service_id
}
}
}
}
I have two types in my schema:
type Resident = { type Visitor = {
id id
name name
} }
In my database:
Residents and Visitors Tables:
+--------+-------+ +--------+---------+
| res_id | name | | vis_id | name |
+--------+-------+ +--------+---------+
| 1 | Alice | | 1 | Charlie |
| 2 | Bob | +--------+---------+
+--------+-------+
And then a table that shows which visitor belongs to which resident:
+--------+--------+--------------+
| res_id | vis_id | relationship |
+--------+--------+--------------+
| 1 | 1 | fam/fri |
| 2 | 1 | contractor |
+--------+--------+--------------+
Each visitor could either be a "fam/fri" or a "contractor" to a resident. So Charlie is Alice's visitor as her family or friend. However, Charlie is also a visitor to Bob, but instead as a contractor.
Question: How do I structure my schema so that when I query Alice, Charlie returns as a fam/fri, and when I query Bob, Charlie is returned as a contractor? I imagine this:
{
Resident(id: 1) { "Resident" {
name "Alice"
Visitor { "Visitor" {
id ===> "1"
name "Charlie"
relationship "fam/fri"
} }
} }
}
and also:
{
Resident(id: 2) { "Resident" {
name "Bob"
Visitor { "Visitor" {
id ===> "1"
name "Charlie"
relationship "contractor"
} }
} }
}
Something like:
type Query {
resident(id: Int): Resident
}
type Resident {
id: Int!
name: String!
visitors: [Visitor!]!
}
type Vistor {
id: Int!
name: String!
relationship: VisitorRelationship!
}
enum VisitorRelationship {
CONTRACTOR
FAMILY_OR_FRIEND
}
Note that by convention field names should be camelCase and type names should be in PascalCase. If the data returned from your data source (whether that's a database, API, or whatever) is not in the same shape as what you want to return from your GraphQL service, then you should transform the data before returning it inside your resolver, for example:
const relationshipMap = {
'fam/fri': 'FAMILY_OR_FRIEND',
'contractor': 'CONTRACTOR',
}
const resolvers = {
Query: {
resident: (root, args) => {
const resident = await Resident.findById(args.id)
// assuming resident has a property named joinTable that's
// an array and each relationship has a property named visitor
return {
...resident,
visitors: resident.joinTable.map(relationship => {
return {
...joinTable.visitor,
relationship: relationshipMap[joinTable.relationship],
}
})
}
},
},
}
You can also map enums to custom values this way.
This is my body/ how the api posts data:
{
"data": {
"email": "string",
"first_name": "string",
"last_name": "string",
}
}
and this is my postProfileRequest struct, which maybe i need to change to accomodate data?
type postProfileRequest struct {
Profile Profile
}
where as this is Profile
type Profile struct {
ID int `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
and i'd like to decode the body request without the data part, so the code below works, since i cant do r.Body.data, i was wondering what would be the best way to do this?
var req postProfileRequest
json.NewDecoder(r.Body).Decode(&req.Profile)
Use the following to decode to a Profile without the data part:
var req postProfileRequest
// Create a value that matches the structure of
// the JSON.
v := struct{ Data *Profile }{&req.Profile}
json.NewDecoder(r.Body).Decode(&v)
fmt.Println(req.Profile) // The data field was decoded to req.Profile
Run it on the playground.