graphql / graphql/graphql-spec

Conditional resolving / optional expanding

Open
#1,000 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

It would be nice if you could somehow make resolving conditional whether i request subfieds at all:

ExampleData:

```
vehicleData = [
{
licenseplate: "AB-CD123",
customerNo: "1234"
},
...
]

customerData = [
{
CusteormNo: "1234",
Name: "John Doe"
}
]

```

How it is
```
schemas:

type vehicle {
licenseplate: String
CustomerNo: String
Customer: Customer
}

type Customer {
CustomerNo: String
Name: String
}

Resolver:

Vehicle: {
Customer: ({ CustomerNo }, args, context, info) => {
return customers.find( x => x.CustomerNo == CustomerNo )
},
},
```

```
query for vehicle without customer:

query ExampleQuery {
vehicles{
licenseplate
CustomerNo
}
}

query for vehicle with customer:

query ExampleQuery {
vehicles{
licenseplate
Customer {
CustomerNo
Name
}
}
}

```

How i wish it would be

```
schemas:

type vehicle {
licenseplate: String
Customer: String | Customer <- dynamic fields?
}

type Customer {
CustomerNo: String
Name: String
}

Resolver:

Vehicle: {
Customer: ({ Customer }, args, context, info) => {
if( _noSubfields_ ) return Customer; /* no expensive resolving */
return customers.find( x => x.Customer== Customer)
},
},
```

```
query for vehicle without customer:

query ExampleQuery {
vehicles{
licenseplate
Customer
}
}

query for vehicle with customer:

query ExampleQuery {
vehicles{
licenseplate
Customer {
CustomerNo
Name
}
}
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.