Ad
Pass Two Variables Into GraphQL Query, One From ReactComponentProps, Other Constructed From First One
I am trying to pass two variables into the following GraphQL query.
import gql from "graphql-tag";
import { TypedQuery } from "../../core/queries";
import { Article, ArticleVariables } from "./types/Article";
const articleQuery = gql`
query Article($slug: String!, $slugimg: String!) {
pageBy(uri: $slug) {
id
pageId
title
date
uri
content(format: RENDERED)
excerpt
}
mediaItemBy(slug: $slugimg) {
uri
sourceUrl
}
}
`;
export const TypedArticleQuery = TypedQuery<Article, ArticleVariables>(
articleQuery
);
using the following code
type ViewProps = RouteComponentProps<{ slug: string }>;
export const View: React.FC<ViewProps> = ({
match: {
params: { slug },
},
}) => (
<TypedArticleQuery loaderFull variables={{ slug, slugimg }} errorPolicy="all">
{({ data }) => {
Is there any way to create a new slugimg variable constructed from the slug variable coming from RouteComponentProps
for example, if the slug variable has value "about-us", then I need the slugimg to have value "about-us-img"
What is the most elegant way of doing this?
Thank you!
Ad
Answer
I believe you can do something like this
<TypedArticleQuery loaderFull variables={{ slug, slugimg:slug ==='about-us'?'about-us-img:'' }} errorPolicy="all">
You didn't state what you wish the value to be when it's not 'about-us' so you can substitute ''
with slug
or whatever you prefer
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad