I've been stuck with this problem in my graphQL API, and I don't know how to unsolved it.
schema.graphQL:
type User @model {
id: ID!
username: String!
email: String!
orders: [Order] @connection(keyName: "byUser", fields: ["id"])
}
type Car @model {
id: ID!
type: String!
latitude: Float,
longitude: Float,
heading: Float,
isActive: Boolean
orders: [Order] @connection(keyName: "byCar", fields: ["id"])
}
type Order @model
@key(name: "byUser", fields: ["userId"])
@key(name: "byCar", fields: ["carId", "createAt"]) {
id:ID!
createAt: ID!
type: String!
originLatitude: Float!
originLongitude: Float!
destLatitude: Float!
destLongitude: Float!
userId: ID! // I think theres something wrong at here.
user: User @connection(fields: ["userId"])
carId: ID!
car: Car @connection(fields: ["carId"])
}
Mutations.js (Generated by AWS):
export const createOrder = /* GraphQL */ `
mutation CreateOrder(
$input: CreateOrderInput!
$condition: ModelOrderConditionInput
) {
createOrder(input: $input, condition: $condition) {
id
createAt
type
originLatitude
originLongitude
destLatitude
destLongitude
userId
user {
id
username
email
orders {
nextToken
}
createdAt
updatedAt
}
carId
car {
id
type
latitude
longitude
heading
isActive
orders {
nextToken
}
createdAt
updatedAt
}
createdAt
updatedAt
}
}
`;
code:
// submit to server
try {
const userInfo = await Auth.currentAuthenticatedUser();
const date = new Date();
const input = {
createAt: date.toISOString(),
type,
originLatitude: originPlace.details.geometry.location.lat || 0,
originLongitude: originPlace.details.geometry.location.lng || 0,
destLatitude: destinationPlace.details.geometry.location.lat || 0,
destLongitude: destinationPlace.details.geometry.location.lng || 0,
userID: userInfo.attributes.sub,
carID: "1",
}
const response = await API.graphql (
graphqlOperation (
createOrder, {
input: input
},
)
)
console.log(response);
Alert.alert (
"Order has been submitted successful!!!",
[{
text: "Go home",
onPress: () => navigation.navigate('HomeScreen')
}]
)
} catch (e) {
console.error(e);
}
}
when I try to submit it to the server it gave me an error.
{"data": null, "errors": [{"locations": null, "message": "The variables input contains a field name 'userID' that is not defined for input object type 'CreateOrderInput' ", "path": null}]}