Skip to content

Relay Modern Example (#1757) #2773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2017
Merged

Relay Modern Example (#1757) #2773

merged 1 commit into from
Aug 14, 2017

Conversation

petrvlcek
Copy link
Contributor

@timneutkens Re-created PR to be merged to master branch this time.

@timneutkens
Copy link
Member

Thanks!

@timneutkens timneutkens merged commit 708193d into vercel:master Aug 14, 2017
@steida
Copy link
Contributor

steida commented Aug 14, 2017

@petrvlcek It doesn't work. Probably wrong path somewhere. Can you fix it please? Also, I don't understand why you chose unfetch, it seems to by only browser polyfil.

@petrvlcek
Copy link
Contributor Author

@steida I forgot to add npm run schema step before npm run relay step into README instructions. I'll create a new pull request with suggested fixes tomorrow.

@steida
Copy link
Contributor

steida commented Aug 14, 2017

And Unfetch does work for Node.js? Anyway, what is the compelling reason to not use isomorphic fetch?

@petrvlcek
Copy link
Contributor Author

petrvlcek commented Aug 14, 2017

It works for Node.js. I've been using it elsewhere and didn't think about it. I have no problem changing that to isomorphic-fetch though, since it has better documentation. PR (#2776) will fix that.

Is the example working after downloading schema introspection data with npm run schema? To simplify it , I'll ditch the schema script and change it to graphql-cli.

@timneutkens
Copy link
Member

@steida isomorphic-unfetch does work on the server. Uses node-fetch under the hood, same as isomorphic-fetch 👌

timneutkens pushed a commit to timneutkens/next.js that referenced this pull request Aug 19, 2017
timneutkens added a commit that referenced this pull request Aug 27, 2017
* Add examples/with-redux-code-splitting. (#2721)

* #1757 Relay Modern Example (#2696)

* Add ReasonML example (#2640)

* Add ReasonML example

* Add a gitignore specifically for the reasonml example

* Allow custom className for <Main /> (#2802)

* 3.0.2

* Remove beta information from the README.

* 3.0.3

* Remove unnecessary lookup in example with emotion (#2731)

* Document SCSS/Less (#2742)

* Document SCSS/Less

* Add missing word

* Add docs for examples dir

* Add extra example

* uppercase J

* Add with pkg example (#2751)

* Add custom server micro example (#2750)

* Ease running multiple examples at the same time with process.env.PORT (#2753)

* Add line-height rule for error page h2 (#2761)

* Add support for fetching multiple translation files (#2743)

* Add support for fetching multiple translation files

* Cleanup

* Clear missed interval (#2611)

* clear missed interval

* remove trailing whitespace

* Relay Modern Example (#1757) (#2773)

* Simplification of Relay Modern Example (#1757) (#2776)

* Use deterministic names for dynamic import (#2788)

* Always use the same name for the same dynamic import.

* Add unit tests for the modulePath generation.

* Allow tests to run correctly on Windows.

* Make the chunk name a bit pretty.

* Fix tests to run on Windows.

* 3.0.4

* Revert "Make the chunk name a bit pretty." (#2792)

This reverts commit 0c9e8cf.

* 3.0.5

* Use _ as the divider for dynamic import name splitter. (#2793)

Using - gives us some weird webpack errors.

* 3.0.6

* next/dynamic Error Message Tweaks (#2798)

* Fixed issue (#2804)

#2800

* docs(material-ui): move the source code to Material-UI repository (#2808)
@ghost
Copy link

ghost commented Jan 16, 2018

I'm trying to inject a postId variable into a fragmentContainer. That variable does not pass at all, even when applying all kind of solutions found on stackoverflow (these don't use nextjs, and use a router, which may be the difference with my project).

I've started the project with the with-relay-modern example.

The variable $postId doesn't inject properly, so I used defaultValue: "UG9zdDoy" which works in the meantime, but the goal is to get the value of postId from props.

Any idea how to achieve this? Is this related to how SSR works?

Deleting **defaultValue: "UG9zdDoy" ** and re-compiling gives the following error:
RelayCompilerScope: No value found for required argument $postId: String!

The page - singlePost.js

import React, { Component } from 'react'
import { graphql } from 'react-relay'
import withData from '_libs/withData'
import PostDetail from '_components/post_page/PostDetail'
import App from '_components/App'

class PostsPage extends Component {
    static displayName = `PostsPage`
    static getInitialProps ({ query }) {
    return { query }
    }

    render (props) {
    const { viewer } = this.props
    return (
            <div>
            {viewer ? <PostDetail {...this.props} 
                        postId={this.props.url.query.postId} 
                        viewer={viewer} 
                        relay={this.props.environment} /> : 'no viewer'}
            </div>
    )
    }
}

export default withData(PostsPage, {
    query: graphql`
    query singlePost_Query { 
        viewer { 
        ...PostDetail_viewer
        } 
    }`,
})

The component - postDetail.js

import React from 'react'
import PropTypes from 'prop-types'
import { createFragmentContainer, graphql } from 'react-relay'

const PostDetail = (props, {viewer}) => {
    return (
    <div>
        post
    </div>
    )
}

PostDetail.propTypes = {
    viewer: PropTypes.shape({
    post: PropTypes.shape({
        title: PropTypes.string.isRequired,
        description: PropTypes.string.isRequired,
        image: PropTypes.string.isRequired,
        creator: PropTypes.shape({
        firstName: PropTypes.string.isRequired,
        lastName: PropTypes.string.isRequired,
        }).isRequired,
    }),
    }).isRequired,
}

// postId only works with defaultValue, but I need to get it from props.
export default createFragmentContainer(
    PostDetail,
    graphql`
    fragment PostDetail_viewer on Viewer
    @argumentDefinitions(
        postId: {type: "String!", defaultValue: "UG9zdDoy"},
    )
    {
        post (postId: $postId) {
        title
        description
        image
        creator {
            firstName
            lastName
        }
        }
    }
    `,
)

@AugustinLF
Copy link
Contributor

I don't remember the exact syntax, but the way I made parameters work for a fragment was to use @arguments on the singlePost_Query side, something like:

query singlePost_Query($postId: String!) { 
        viewer { 
        ...PostDetail_viewer @arguments(postId: $postId)
        } 
    }

And then you pass your postId param to your withData. If this doesn't work for you, I'll try to find you a more complete example, but my code is slightly more complicated than that.

@ghost
Copy link

ghost commented Jan 16, 2018

@AugustinLF many thanks for your answer.
withData could indeed be the problem, how should I pass the postId to withData?

@ghost
Copy link

ghost commented Jan 16, 2018

Ok got it, just in case someone needs this in the future:
As @AugustinLF said

// singlePost.js
query singlePost_Query($postId: String!) { 
    viewer { 
        ...PostDetail_viewer @arguments(postId: $postId)
    } 
}

Then in withData.js, add postId in the variables

if (options.query) {
        const variables = {
          postId: ctx.query.postId
        }
        queryProps = await fetchQuery(environment, options.query, variables)
        queryRecords = environment.getStore().getSource().toJSON()
}

@tusgavomelo
Copy link

tusgavomelo commented Jan 29, 2018

Hi! I'm having a little trouble to get the errors of a query using this example.

The errors returned by the query are not available on the component props.

// query response
{
  data: { foo: null },
  errors: [
    { message: 'example' },
  ]
}
// component render method
render () {
  console.log(this.props.foo) // null
  console.log(this.props.errors) // undefined :(
}

@lock lock bot locked as resolved and limited conversation to collaborators Jan 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants