Appnigma

How to Use OR in GraphQL Salesforce

salesforce managed package

Nov 21, 2025

4 min read

How to Use OR in GraphQL Salesforce

Salesforce has made huge progress by introducing GraphQL as part of its modern API layer. If you're used to SOQL or REST API queries, GraphQL may feel like a breath of fresh air—especially when it comes to writing flexible and powerful filters.

One of the most common things developers need is applying OR logic to multiple fields or conditions. In SOQL, this is simple. But in GraphQL, the structure is different and often confusing at first.

This guide walks you through exactly how to use OR in Salesforce GraphQL with examples, best practices, and common mistakes to avoid.

Introduction to GraphQL in Salesforce

What is Salesforce GraphQL?

The Salesforce GraphQL API is a modern query language designed to fetch exactly the data you need in a single request. It is flexible, developer-friendly, and efficient compared to traditional APIs.

For deeper context, refer to the Salesforce GraphQL API documentation:

Why Salesforce Introduced GraphQL

Salesforce introduced GraphQL to address issues such as:

  • Multiple REST calls

  • Complicated joins

  • Heavy payloads

  • Difficult data shaping

Benefits Over SOQL and REST

GraphQL offers several advantages:

  • Fetch nested data in a single call

  • Retrieve only the fields you want

  • Combine logic filters easily

  • Better performance with large datasets

Understanding Filters in Salesforce GraphQL

Basic Structure of Filters

GraphQL uses object-based filtering instead of SQL-like operators.

filter: { name: { eq: "John Doe" } }

AND vs OR Filters

  • AND is applied automatically when multiple conditions are placed in the same filter block.

  • OR must be defined explicitly using an array.

Learn more from the GraphQL official site:
https://graphql.org/learn/

How OR Logic Works in Salesforce GraphQL

Syntax for Using OR

filter: { or: [ { Name: { eq: "John" } }, { Name: { eq: "Jane" } } ] }

Nesting OR and AND Together

filter: { and: [ { Status: { eq: "Active" } }, { or: [ { Country: { eq: "USA" } }, { Country: { eq: "Canada" } } ] } ] }

Common Use Cases

  • Filtering contacts by multiple countries

  • Returning accounts from different industries

  • Getting leads with several matching statuses

Practical Examples of Using OR in Salesforce GraphQL

Example 1 – Simple OR Condition

query { uiapi { query { Contact( filter: { or: [ { FirstName: { eq: "John" } }, { FirstName: { eq: "David" } } ] } ) { edges { node { Id Name } } } } } }

Example 2 – OR with Multiple Fields

filter: { or: [ { Email: { like: "%gmail.com" } }, { Phone: { eq: "1234567890" } } ] }

Example 3 – Combining AND + OR

filter: { and: [ { IsActive: { eq: true } }, { or: [ { Industry: { eq: "Tech" } }, { Industry: { eq: "Finance" } } ] } ] }

Example 4 – OR with Relationships

filter: { or: [ { Account: { Name: { eq: "Salesforce" } } }, { Account: { Industry: { eq: "Software" } } } ] }

Explore more Salesforce development articles at:
https://appnigma.ai/blogs/

Limitations of OR Filters in Salesforce GraphQL

Query Size Restrictions

Large OR arrays may lead to:

  • Query too large errors

  • API timeouts

Too Much Nesting

Salesforce limits how deeply filters can be nested.

Unsupported Fields

Some fields do not support every filter operator.

Reference Salesforce entity documentation:
https://developer.salesforce.com/docs/

Best Practices for Using OR in Salesforce GraphQL

Keep Filters Clean

Use arrays for clarity and maintainability.

Use Nested Logic Wisely

Avoid unnecessary AND/OR nesting.

Optimize for Performance

Fetch only the fields required.

Learn how Appnigma accelerates native Salesforce development:
https://appnigma.ai/

Troubleshooting Common OR Errors

Invalid Filter Format

Incorrect:

or: { Name: { eq: "John" } }

Correct:

or: [ { Name: { eq: "John" } } ]

Too Many Nested Conditions

Break complex logic into separate queries when required.

Unsupported Field Types

Some formula fields may behave differently.

Conclusion

Using OR in Salesforce GraphQL becomes simple once you understand the structure. GraphQL uses nested objects and arrays for logical expressions rather than SQL-like syntax. Once you adapt to this approach, building flexible, modern Salesforce queries becomes much easier.

GraphQL is becoming the preferred Salesforce API layer, and mastering OR filters is essential for scalable development.

Further reading:
https://appnigma.ai/

FAQs

1. Can OR be used across multiple object fields?

Yes, OR can be applied across any supported fields.

2. Can I mix OR and AND in the same filter?

Yes, using nested arrays.

3. Are OR filters slower in Salesforce GraphQL?

Not always, but very large OR arrays may reduce performance.

4. Is GraphQL replacing SOQL?

No. SOQL still powers Salesforce internally, but GraphQL is the modern API layer.

5. How do I test OR conditions easily?

Use the Salesforce GraphQL Playground or API Explorer.

Ready to transform your Salesforce experience?

Start exploring the Salesforce Exchange today and discover apps that can take your CRM efficiency to the next level.

decorative section tag

Blog and News

Our Recent Updates