Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-06-2019 04:04 AM
Alright,
here is the issue, a plan object has some properties, a relation to a provider, and that provider has some other properties. In Playground, I can query as usual and get both, the properites from the plan and from the provider. However, when using Apollo Client, with the same query, I get an empty object.
How do I solve that to access the Provider?
Schema:
(Additional fields omitted for brevity)
type HealthPlan {
name: String!
plan_id: ID!
plan_provider: HEALTH_PLAN_HAS_Provider
}
# HealthPlan [HEALTH_PLAN_HAS_Provider] Provider
type HEALTH_PLAN_HAS_Provider @relation(name: "HEALTH_PLAN_HAS_Provider") {
from: HealthPlan!
to: Provider!
}
type Provider{
provider_id: INSURANCE_PROVIDER! # Enum with unique id's.
name: String!
}
Working query in playground:
query{
healthPlanByID(plan_id:"smj-plan-c-23"){
plan_type
name
plan_rate
plan_id
plan_provider{
Provider{provider_id name}
}
}
}
Executing the query above returns exactly the expected result:
{
"data": {
"healthPlanByID": {
"plan_type": "HCP",
"name": "Plan C",
"plan_rate": 193000,
"plan_id": "smj-plan-c-23",
"plan_provider": {
"Provider": {
"provider_id": "SMJ",
"name": "Simas Jiwa"
}
}
}
}
}
However, the client queries the API using the Apollo client to run the same query.
To do so, the query is embedded in an async function that passes the query to the client:
const client = new ApolloClient({
link: new HttpLink({uri: process.env.GRAPHQL_URI, fetch}),
cache: new InMemoryCache(),
});
async function getPlanData(plan_ID) {
const QPLAN = gql`
query plan($id: String!) {
healthPlanByID(plan_id: $id){plan_type,
name, plan_rate, plan_id
plan_provider{
Provider{
provider_id,
name
}
}
}
}`;
let res = await client.query({query: QPLAN, variables: {id: plan_ID},});
console.log("PLAN QUERY RESULT", res);
return res;
}
That async function executes the query but returns only an object for the Provider relation:
PLAN QUERY RESULT {
healthPlanByID: {
plan_type: 'HCP',
name: 'Plan C',
plan_rate: 193000,
plan_id: 'smj-plan-c-23',
plan_provider: { Provider: [Object], __typename: '_HealthPlanPlan_provider' },
__typename: 'HealthPlan'
}
}
When I try to access the fields in the Provider object anyway, I get "undefined" returned.
I have the situation of requesting nested objects quite often and I bump into this issue virtually every time but it seems strange to me that each time the query works fine int he playground but not in the Apollo client. Why is that?
Question:
How can I access the fields inside the Provider object by using the Apollo client?
Solved! Go to Solution.
12-06-2019 04:48 AM
If your relationship doesn't have any properties on it the best way to express that in the schema would be:
type HealthPlan {
name: String!
plan_id: ID!
plan_provider: Provider @relation(
name: "HEALTH_PLAN_HAS_PROVIDER", direction: "OUT")
}
type Provider{
provider_id: INSURANCE_PROVIDER! # Enum with unique id's.
name: String!
plan: HealthPlan @relation(
name: "HEALTH_PLAN_HAS_PROVIDER", direction: "IN")
}
Once the relationship is created you will then be able to get it correctly returned. I know that doesn't answer your question about the provider object, but I think it'll get you where you want to be.
12-06-2019 04:48 AM
If your relationship doesn't have any properties on it the best way to express that in the schema would be:
type HealthPlan {
name: String!
plan_id: ID!
plan_provider: Provider @relation(
name: "HEALTH_PLAN_HAS_PROVIDER", direction: "OUT")
}
type Provider{
provider_id: INSURANCE_PROVIDER! # Enum with unique id's.
name: String!
plan: HealthPlan @relation(
name: "HEALTH_PLAN_HAS_PROVIDER", direction: "IN")
}
Once the relationship is created you will then be able to get it correctly returned. I know that doesn't answer your question about the provider object, but I think it'll get you where you want to be.
12-06-2019 05:01 AM
Thanks, that really is the answer.
However, I have to implement it slightly differently due to some particularities in the object hierarchy unmentioned in the original post... That is how it is.
All the sessions of the conference are now available online